-1

In PHP 5.6:

When being inside a class we usually declare & call a public class variable like this:

class MyClass
{
    /**
     * @var int
     */
    public $myVar = 0;

    // call it in a public function:
   public function myFunction()
   {
       return $this->myVar;
   }

}

I call the function like this:

MyClass::myFunction();

In PHP 7.0 that code throws a fatal error:

Using $this when not in object context

After changing my PHP version back to 5.6 again, the error was gone.

Questions:

I have to admit that after reading the manual and changes from 5.6 to 7.0 I don't get it.

  1. How do we declare and call public class variables in PHP 7.0?
  2. How do we write this code to be compatible between 5.6 and 7.0?

Edit after comments:

So why then a static call on a non static method works in 5.6?

toesslab
  • 5,092
  • 8
  • 43
  • 62
  • 2
    How are you calling the function? That's important. – Niet the Dark Absol May 06 '16 at 10:02
  • 1
    I'm rather sure you're analyzing the error incorrectly. The code you are showing is fine. I agree with the above that it is the calling of said method that is the cause. – Anees Saban May 06 '16 at 10:04
  • Added the function call – toesslab May 06 '16 at 10:04
  • 1
    It's not a static function, and if it was. Your variable is not static. – Anees Saban May 06 '16 at 10:05
  • Ok I get it now, but why this works in 5.6? – toesslab May 06 '16 at 10:05
  • 2
    http://php.net/manual/en/migration70.deprecated.php - Static calls to non-static methods – Anees Saban May 06 '16 at 10:06
  • I've just tested in both, php5 and php7. There is no difference in behavior. – Pinke Helga May 06 '16 at 10:18
  • 1
    It doesn't work as you want in 5.6, but you're probably suppressing the messages in your 5.6... 7 has made the messages stricter [Demo](https://3v4l.org/cvgP2) – Mark Baker May 06 '16 at 10:29
  • I've setup a test case, where this fatal error occurs. This is when you already are in a $this context. – Pinke Helga May 06 '16 at 10:39
  • @MarkBaker Try out my example below. It throws a fatal error in 7 but not even a warning in 5. – Pinke Helga May 06 '16 at 10:49
  • Not even a warning?!? 5.6 gives `Deprecated: Non-static method MyClass::myFunction() should not be called statically, assuming $this from incompatible context in /in/T4Qbm on line 26 2` and 5.5 gives `Strict Standards: Non-static method MyClass::myFunction() should not be called statically, assuming $this from incompatible context in /in/T4Qbm on line 26 2` [Demo](https://3v4l.org/T4Qbm) – Mark Baker May 06 '16 at 10:54
  • @MarkBaker Check, in my CLI version error reporting had become disabled for some reason, probably due to an update. – Pinke Helga May 06 '16 at 11:12

3 Answers3

5

In I'm loading maybe func() like this:

 obj::func();  // Wrong, it is not static method

but can also be

$obj = new Obj();  // correct
$obj->func();

You can not invoke method this way because it is not static method.

obj::func();

You should instead use:

obj->func();

If however you have created a static method something like:

static $foo; // your top variable set as static

public static function foo() {
    return self::$foo;
}

then you can use this:

obj::func();
DevLoots
  • 747
  • 1
  • 6
  • 21
1

The behavior you describe can be found in the following example:

<?php

class MyClass
{
  /**
   * @var int
   */
  public $myVar = 1;

  // call it in a public function:
  public function myFunction()
  {
    return $this->myVar;
  }    
}

class MyClass2
{
  /**
   * @var int
   */
  public $myVar = 2;

  public function test()
  {
    echo MyClass::myFunction();
    // outputs: 2
  }
}


$obj = new MyClass2();
$obj->test();
?>

In PHP 5 you can call a public method of other classes, when you are inside an instance. It will work as it was a member of the class in the current $this context. PHP 7 is more stict. You can extend classes or import traits into classes in different inheritance lines. There is no need to steel other classes' methods. You can still use the Classname::method() or parent::method() syntax to call ancestor's methods.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
0

You accomplish the same by instantiating object of that class and then call that class's method.

class MyClass
{
    /**
     * @var int
     */
    public $myVar = 0;

    // call it in a public function:
   public function myFunction()
   {
       return $this->myVar;
   }

}
$obj = new Myclass();
$result = $obj->myFunction();
print $result;
Sagar
  • 642
  • 3
  • 14