假设有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能实现这个效果么?
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();
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(); @$v++;
问一下这句为什么要加“@”符合?
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号