足够简单
class base_class { public function doSomethingWithReference(){ $this->reference->doSomething(); } } class extended_class extends base_class{ protected $reference; public function __construct($ref){ $this->reference = $ref; } }
现在这样很好,
但是,当我调试时,我不关心$this->引用的值
但是,$this->引用所指的对象是巨大的!
所以当我做print_r($instanceOfExtendedClass)时,我得到该对象的打印.
现在对于扩展base_class的每个类的引用是不同的.
我想做的只是将引用设置为extended_class类上的静态属性.
但是,然后将doSomethingWithReference更改为self :: $reference引发一个未定义的变量错误.
相反,在base_class中设置静态变量并且从extended_class修改它将不起作用,因为它会从该类扩展的所有内容更改变量.
有没有办法这样做,所以我不能从$this->引用中获得打印?
class base_class { public function doSomethingWithReference(){ static::$reference->doSomething(); } } class extended_class extends base_class{ protected static $reference; public function __construct($ref){ static::$reference = $ref; } }
大胖提醒:一个extended_class :: $引用将在所有的extended_class实例之间共享.如果这不是你打算的话,这是不行的.
你似乎真的担心内存或资源的使用.在PHP中,所有对象都通过引用传递.这意味着传递一个对象作为一个参数,或创建一个它的副本等,不会消耗额外的内存.如果您需要引用一些其他对象的对象,这样做不会消耗额外的内存.
If I had extended_class and another identical class (say extended_class1) would they share the reference as well? or would all extended_class’ instances share one reference,while all extended_class1′ instances would share another (the ideal case)?
看起来共享是基于定义静态变量的位置.两个例子,都是从PHP交互提示:
PHP > class Shared { public $me; public function __construct($me) { $this->me = $me; } } PHP > class Base { protected static $ref; public function foo() { echo static::$ref->me,"\n"; } } PHP > class Inherit_1 extends Base { public function __construct($ref) { static::$ref = $ref; } } PHP > class Inherit_2 extends Base { public function __construct($ref) { static::$ref = $ref; } } PHP > class Inherit_3 extends Inherit_1 {} PHP > $shared_1 = new Shared(1) PHP > ; PHP > $shared_2 = new Shared(2); PHP > $shared_3 = new Shared(3); PHP > PHP > $in_1 = new Inherit_1($shared_1); PHP > $in_2 = new Inherit_2($shared_2); PHP > $in_3 = new Inherit_3($shared_3); PHP > PHP > $in_1->foo(); 3 PHP > $in_2->foo(); 3 PHP > $in_3->foo(); 3
在这种情况下,由于引用存在于基类中,所以每个人都看到相同的引用.我想这有点有道理.
当我们声明每个子类的引用时,大部分时间会发生什么?
PHP > class Shared { public $me; public function __construct($me) { $this->me = $me; } } PHP > class Base { public function foo() { echo static::$ref->me,"\n"; } } PHP > class Inherit_1 extends Base { protected static $ref; public function __construct($ref) { static::$ref = $ref; } } PHP > class Inherit_2 extends Base { protected static $ref; public function __construct($ref) { static::$ref = $ref; } } PHP > class Inherit_3 extends Inherit_1 {} PHP > class Inherit_4 extends Inherit_1 { protected static $ref; } PHP > $shared_1 = new Shared(1); PHP > $shared_2 = new Shared(2); PHP > $shared_3 = new Shared(3); PHP > $shared_4 = new Shared(4); PHP > $in_1 = new Inherit_1($shared_1); PHP > $in_2 = new Inherit_2($shared_2); PHP > $in_3 = new Inherit_3($shared_3); PHP > $in_4 = new Inherit_4($shared_4); PHP > $in_1->foo(); 3 PHP > $in_2->foo(); 2 PHP > $in_3->foo(); 3 PHP > $in_4->foo(); 4
因为3从1继承而没有声明它是自己的静态属性,它继承了1.当我们将3设置为Shared(3)时,它覆盖了1个现有的Shared(1).