Bug #68485 [Nab]: late namespace resolution with cross-namespace inheritance missing

From: Date: Sun, 23 Nov 2014 23:43:12 +0000
Subject: Bug #68485 [Nab]: late namespace resolution with cross-namespace inheritance missing
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188766@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68485&edit=1 ID: 68485 Updated by: requinix@php.net Reported by: llmll at gmx dot de Summary: late namespace resolution with cross-namespace inheritance missing Status: Not a bug Type: Bug Package: Class/Object related Operating System: any PHP Version: 5.5.19 Block user comment: N Private report: N New Comment: That may be but it's still not a bug. There is no such feature like what you're describing; the closest is LSB which is why I brought it up. Your options include a) Beta\Base redefining Write() to use the Helper class that it wants to use. b) (Ab)using get_called_class() to extract the called namespace, test that the corresponding Helper class exists, and use its $Value. c) Dependency injection, which sounds like the best solution to whatever you're trying to do. Previous Comments: ------------------------------------------------------------------------ [2014-11-23 23:03:31] llmll at gmx dot de Sorry to say, but you did not understand the example provided. Please go through it again and find that it has nothing to do with late static binding. Your suggestion is invalid. A static::$Value is not defined, as we want the $Value property of a different class: \Alpha\Helper. So we access it as Helper::$Value. And here comes the problem. When later both classes are inherited / copied to \Beta namespace, the Helper::$Value readout still accesses \Alpha\Helper::$Values, but it should be using \Beta\Helper::$Value. ------------------------------------------------------------------------ [2014-11-23 22:35:30] requinix@php.net LSB happens with the static keyword, not class names. Classes always resolve according to the namespace of the code being executed (or by use statements), not some kind of contextual namespace dependent on where the code is being executed. echo static::$Value; Don't forget that trying to access an undefined static property will fatal. ------------------------------------------------------------------------ [2014-11-23 21:50:26] llmll at gmx dot de Description: ------------ Since PHP 5.3 we have late static binding to get a static variable from the actually instantiated class type instead of the declared type. This is an important feature, but it does not take into account a change in namespace. If the inheriting class lives in another namespace the wrong value is taken. If inside a namespace a class uses a peer class by its relative name, and later both classes are derived in another namespace, all accesses go to the original namespace where the class was declared in the first place. Neccessary would be a late (runtime) namespace resolution for class names, the same was it works for static:: Test script: --------------- <?php namespace Alpha; class Base { public static function Write() { echo Helper::$Value; } } class Helper { public static $Value = "ALPHA"; } namespace Beta; class Base extends \Alpha\Base { } class Helper extends \Alpha\Helper { public static $Value = 'BETA'; } \Beta\Base::Write(); ?> Expected result: ---------------- As the exmple calls \Beta\Base::Write() in \Beta namespace one would expect the output to be "BETA". The original static property is overwritten in Beta-namespace The calls Helper::$Value should be call-context-sensitive. If called in namespace \Alpha it shoult print "ALPHA" if called from a \Beta class, "BETA". Actual result: -------------- prints "ALPHA" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68485&edit=1

« previous php.bugs (#188766) next »