Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91254 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75866 invoked from network); 17 Feb 2016 18:16:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Feb 2016 18:16:09 -0000 X-Host-Fingerprint: 2.121.171.145 unknown Received: from [2.121.171.145] ([2.121.171.145:4330] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/4C-17120-7E8B4C65 for ; Wed, 17 Feb 2016 13:16:08 -0500 Message-ID: <31.4C.17120.7E8B4C65@pb1.pair.com> To: internals@lists.php.net References: Date: Wed, 17 Feb 2016 18:16:04 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:42.0) Gecko/20100101 Firefox/42.0 SeaMonkey/2.39 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 2.121.171.145 Subject: Re: Class constants on object references have variable-inconsistent accessibility From: ajf@ajf.me (Andrea Faulds) 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: > > > 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: > > > 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: 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/