note 109639 modified in language.oop5.late-static-bindings by danbrown
| From: | danbrown@php.net | 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'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 ?>
--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