Class variables, scope resolution operator and different PHP versions

I tried the following code at codepad.org:

class test { 
  const TEST = 'testing 123';
  function test () {
    $testing = 'TEST';
    echo self::$testing;
  }
} 
$class = new test;

      

And he came back with:

1
2 Fatal error: Access to undeclared static property:  test::$testing on line 6

      

I want to know if a class constant reference with a variable will work on my server at home which runs php 5.2.9 whereas the codec uses 5.2.5. What are the changes in class variables with each PHP version?

0


source to share


1 answer


The scope resolution operator (also called Paamayim Nekudotaim), or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden members or methods of a class.

The variable you define in the function test ($ testing) is not static or constant, so the scope resolution operator does not apply.

class test { 
  const TEST = 'testing 123';
  function test () {
    $testing = 'TEST';
    echo $testing;
  }
} 

$class = new test;

      



Or just access the constant outside the class:

test::TEST;

      

It should work on your server at home if used correctly. For the OOP changes from PHP4 to PHP5, the php documentation might be helpful . Although I would say that the main changes to PHP5 related to class variables will be their visibility, statics and constants. All of them are described in the attached documentation.

+3


source







All Articles