Class constants on object references have variable-inconsistent accessibility

php.internals

Jakub Kubíček

10 years ago
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

Andrew Faulds

10 years ago
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/

Jakub Kubíček

10 years ago
Hi Andrea, I am sorry I haven't checked this before. I am using 5.5.12 as that's also the production environment I am mostly working with. I am planning upgrade to 7.x in a long-term. Anyways, thanks for correcting me. Very best regards, Kubis Pandian-Fowler Od: Andrea Faulds Odoslané: ‎streda‎, ‎17‎. ‎februára‎ ‎2016 ‎23‎:‎46 Komu: internals@lists.php.net 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/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php