关于initialize如何实现
程序员文章站
2024-04-04 16:15:17
...
假设有class a、b、base,a、b都继承base,
a、b允许有一个方法initialize();
如果new N次:
$obj = new a();
$obj = new a();
$obj = new a();
$obj = new b();
$obj = new b();
$obj = new b();
只执行一次 a->initialize(); 和一次 b->initialize();
initialize在框架中主要是替代__construct方法。
phalcon实现了这个功能,但是phalcon是C编写的框架,纯PHP能实现这个效果么?
a、b允许有一个方法initialize();
如果new N次:
$obj = new a();
$obj = new a();
$obj = new a();
$obj = new b();
$obj = new b();
$obj = new b();
只执行一次 a->initialize(); 和一次 b->initialize();
initialize在框架中主要是替代__construct方法。
phalcon实现了这个功能,但是phalcon是C编写的框架,纯PHP能实现这个效果么?
回复讨论(解决方案)
class bass { function __construct() { $this->_initialize(); } function _initialize() { static $v; @$v++; if($v == 1 && method_exists($this, 'initialize')) $this->initialize(); }}class a extends bass { function initialize() { echo __CLASS__; }}class b extends bass { function initialize() { echo __CLASS__; }}$obj = new a();$obj = new a();$obj = new a();$obj = new b();$obj = new b();$obj = new b();ab
class bass { function __construct() { $this->_initialize(); } function _initialize() { static $v; @$v++; if($v == 1 && method_exists($this, 'initialize')) $this->initialize(); }}class a extends bass { function initialize() { echo __CLASS__; }}class b extends bass { function initialize() { echo __CLASS__; }}$obj = new a();$obj = new a();$obj = new a();$obj = new b();$obj = new b();$obj = new b();ab
@$v++;
问一下这句为什么要加“@”符合?