note 120507 deleted from language.oop5.static by cmb
| From: | cmb@php.net | Date: | Sun, 01 Sep 2019 07:37:20 +0000 |
| Subject: | note 120507 deleted from language.oop5.static by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212062@lists.php.net to get a copy of this message | ||
Note Submitter: alberto at albertoareba dot es
----
Be careful while calling static constants or methods dynamically with a variable. While you can call
them since PHP 5.3.0 though a simple variable, it will throw a "Syntax error" when using
an object property. This issue is fixed in PHP 7.
<?php
class Foo
{
const BAR = 'const';
public static function bar()
{
return 'static';
}
}
echo Foo::BAR . PHP_EOL; // Prints "const"
echo Foo::bar() . PHP_EOL; // Prints "static"
$var = 'Foo';
echo $var::BAR . PHP_EOL; // Prints "const" in PHP 5.3.0, "Syntax error" in
older versions
echo $var::bar() . PHP_EOL; // Prints "static" in PHP 5.3.0, "Syntax error" in
older versions
$object = new stdClass;
$object->class = 'Foo';
echo $object->class::BAR . PHP_EOL; // "Syntax error" in PHP 5.3.0, prints
"const" in PHP 7
echo $object->class::bar() . PHP_EOL; // "Syntax error" in PHP 5.3.0, prints
"static" in PHP 7
?>