Dear internals!
I have encountered an inconsistence, or rather a bug, in PHP's syntax.
The thing is, in PHP you can access constants defined on a
class using reference to an instance of this class, stored in
variable. So we have a code:
<?php
class B {
const C = 4;
}
$b = new B;
var_dump($b::C === B::C); // bool(true)
?>
Which will nicely pass. But things will go wrong having a code trying
to access the same constant on an object of B stored in member
variable of class A -- let's see below:
<?php
class A {
/** @var B */
public $b;
}
$a = new A;
$a->b = new B;
var_dump($a->b::C === B::C); // Parse error: syntax error, unexpected
'::' (T_PAAMAYIM_NEKUDOTAYIM) in...
?>
Let me conclude that if there's access to class' constant using a
reference to an instance of the class, it should be possible with any
type of value-holder (so-called variable).
--
Cheers,
Kubis
Hi Jakub,
Jakub Kubíček wrote:
I have encountered an inconsistence, or rather a bug, in PHP's syntax.
The thing is, in PHP you can access constants defined on a
class using reference to an instance of this class, stored in
variable. So we have a code:<?php
class B {
const C = 4;
}$b = new B;
var_dump($b::C === B::C); // bool(true)?>
Which will nicely pass. But things will go wrong having a code trying
to access the same constant on an object of B stored in member
variable of class A -- let's see below:<?php
class A {
/** @var B */
public $b;
}$a = new A;
$a->b = new B;
var_dump($a->b::C === B::C); // Parse error: syntax error, unexpected
'::' (T_PAAMAYIM_NEKUDOTAYIM) in...?>
Let me conclude that if there's access to class' constant using a
reference to an instance of the class, it should be possible with any
type of value-holder (so-called variable).
Which versions of PHP have you tried? The following works in PHP 7:
<?php
class B {
const C = 4;
}
class A {
/** @var B */
public $b;
}
$a = new A;
$a->b = new B;
var_dump($a->b::C === B::C);
?>
It outputs:
bool(true)
PHP 7 dealt with a lot of issues like these with the Uniform Variable
Syntax RFC:
https://wiki.php.net/rfc/uniform_variable_syntax
Unfortunately, if you want that code to work, you'll have to upgrade to
PHP 7, because there's no plans for another 5.x release, and this isn't
a simple bug fix.
Thanks.
--
Andrea Faulds
https://ajf.me/