note 88681 added to language.oop5.paamayim-nekudotayim

From: Date: Tue, 03 Feb 2009 17:54:02 +0000
Subject: note 88681 added to language.oop5.paamayim-nekudotayim
Groups: php.notes 
Request: Send a blank email to php-notes+get-150153@lists.php.net to get a copy of this message
For the 'late static binding' topic I published a code below, that demonstrates a trick for how to setting variable value in the late class, and print that in the parent (or the parent's parent, etc.) class. <?php class cA { /** * Test property for using direct default value */ protected static $item = 'Foo'; /** * Test property for using indirect default value */ protected static $other = 'cA'; public static function method() { print self::$item."\r\n"; // It prints 'Foo' on everyway... :( print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :) } public static function setOther($val) { self::$other = $val; // Set a value in this scope. } } class cB extends cA { /** * Test property with redefined default value */ protected static $item = 'Bar'; public static function setOther($val) { self::$other = $val; } } class cC extends cA { /** * Test property with redefined default value */ protected static $item = 'Tango'; public static function method() { print self::$item."\r\n"; // It prints 'Foo' on everyway... :( print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :) } /** * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun. */ } class cD extends cA { /** * Test property with redefined default value */ protected static $item = 'Foxtrot'; /** * Now we drop redeclaring all methods to complete this issue. */ } cB::setOther('cB'); // It's cB::method()! cB::method(); // It's cA::method()! cC::setOther('cC'); // It's cA::method()! cC::method(); // It's cC::method()! cD::setOther('cD'); // It's cA::method()! cD::method(); // It's cA::method()! /** * Results: -> * Foo * cB * Tango * cC * Foo * cD * * What the hell?! :) */ ?> ---- Server IP: 92.43.200.200 Probable Submitter: 80.98.24.192 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php Edit -- https://master.php.net/note/edit/88681 Del: integrated -- https://master.php.net/note/delete/88681/integrated Del: useless -- https://master.php.net/note/delete/88681/useless Del: bad code -- https://master.php.net/note/delete/88681/bad+code Del: spam -- https://master.php.net/note/delete/88681/spam Del: non-english -- https://master.php.net/note/delete/88681/non-english Del: in docs -- https://master.php.net/note/delete/88681/in+docs Del: other reasons-- https://master.php.net/note/delete/88681 Reject -- https://master.php.net/note/reject/88681 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150153) next »