note 109639 modified in language.oop5.late-static-bindings by danbrown

From: Date: Fri, 10 Aug 2012 16:16:14 +0000
Subject: note 109639 modified in language.oop5.late-static-bindings by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190010@lists.php.net to get a copy of this message
For static properties declared in parent class and not redeclared in sub-classes, static:: in parent class would hit parent&#39;s properies. (class a and b below)&#13;&#10;If those were declared in sub-classes, static:: in parent class will hit sub-classes&#39;s own. (class c and d below).&#13;&#10;&#13;&#10;e.g.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;$_CONFIG[&#39;a&#39;] = &#39;1&#39;;&#13;&#10;$_CONFIG[&#39;b&#39;] = &#39;2&#39;;&#13;&#10;$_CONFIG[&#39;c&#39;] = &#39;3&#39;;&#13;&#10;$_CONFIG[&#39;d&#39;] = &#39;4&#39;;&#13;&#10;&#13;&#10;class static_base&#13;&#10;{&#13;&#10; static protected $config;&#13;&#10; static protected $module_name = &#39;&#39;;&#13;&#10; static public function load()&#13;&#10; {&#13;&#10; global $_CONFIG;&#13;&#10; static::$config = $_CONFIG[static::$module_name];&#13;&#10; }&#13;&#10; static public function dump()&#13;&#10; {&#13;&#10; var_dump(static::$config);&#13;&#10; }&#13;&#10;}&#13;&#10;&#13;&#10;class a extends static_base&#13;&#10;{&#13;&#10; static protected $module_name = &#39;a&#39;;&#13;&#10;}&#13;&#10;&#13;&#10;class b extends static_base&#13;&#10;{&#13;&#10; static protected $module_name = &#39;b&#39;;&#13;&#10;}&#13;&#10;&#13;&#10;class c extends static_base&#13;&#10;{&#13;&#10; static protected $module_name = &#39;c&#39;;&#13;&#10; static protected $config;&#13;&#10;}&#13;&#10;class d extends static_base&#13;&#10;{&#13;&#10; static protected $module_name = &#39;d&#39;;&#13;&#10; static protected $config;&#13;&#10;}&#13;&#10;&#13;&#10;a::load();&#13;&#10;b::load();&#13;&#10;a::dump(); // 2&#13;&#10;b::dump(); // 2&#13;&#10;&#13;&#10;c::load();&#13;&#10;d::load();&#13;&#10;c::dump(); // 3&#13;&#10;d::dump(); // 4&#13;&#10;?&#62; --was-- For static properties declared in parent class and not redeclared in sub-classes, static:: in parent class would hit parent's properies. (class a and b below) If those were declared in sub-classes, static:: in parent class will hit sub-classes's own. (class c and d below). e.g. <?php $_CONFIG['a'] = '1'; $_CONFIG['b'] = '2'; $_CONFIG['c'] = '3'; $_CONFIG['d'] = '4'; class static_base { static protected $config; static protected $module_name = ''; static public function load() { global $_CONFIG; static::$config = $_CONFIG[static::$module_name]; } static public function dump() { var_dump(static::$config); } } class a extends static_base { static protected $module_name = 'a'; } class b extends static_base { static protected $module_name = 'b'; } class c extends static_base { static protected $module_name = 'c'; static protected $config; } class d extends static_base { static protected $module_name = 'd'; static protected $config; } a::load(); b::load(); a::dump(); // 2 b::dump(); // 2 c::load(); d::load(); c::dump(); // 3 d::dump(); // 4 http://php.net/manual/en/language.oop5.late-static-bindings.php

« previous php.notes (#190010) next »