This question made me curious about using language constructs in combination with PHP's magic methods. I have created a demo code:
<?php
class Testing {
public function scopeList() {
echo "scopeList";
}
public function __call($method, $parameters) {
if($method == "list") {
$this->scopeList();
}
}
public static function __callStatic($method, $parameters) {
$instance = new static;
call_user_func_array([$instance, $method], $parameters);
}
}
//Testing::list();
$testing = new Testing();
$testing->list();
Why does Testing::list()
throw a syntax error and $testing->list()
does not?
Due to php reserved keywords both should fail?
Context sensitive identifiers are now supported for PHP 7.0+ and your code would simply work. Updating your PHP would fix the issue.
This was the approved RFC that made the change: https://wiki.php.net/rfc/context_sensitive_lexer.
You can get more information about new features and breaking changes on the following (unofficial) PHP 7 reference: https://github.com/tpunt/PHP7-Reference#loosening-reserved-word-restrictions
Update PHP 7
PHP 7 addressed the described behaviour and implemented a feature called context sensitive lexer as brought up by marcio.
Your code will simply work with PHP 7.
Situation before PHP 7
Syntax errors are thrown before PHP is even aware of the fact that a method is available through __callStatic()
, it happens at parsing stage.
The behaviour you described seems to be a bug in the PHP parser, at least an inconsistency that should be described in the documentation.
I would file a bug report. Good catch!
Update: The OP has file a bug report which can be found here: https://bugs.php.net/bug.php?id=71157
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With