gpt4 book ai didi

php - 从父类访问子类静态变量?

转载 作者:行者123 更新时间:2023-12-02 10:05:25 24 4
gpt4 key购买 nike

我有一个基类,我需要调用子类中引用的类上的函数。

很简单,

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->reference 的值

但是,$this->reference引用的对象是巨大的!

所以,当我执行 print_r($instanceOfExtendedClass) 时,我会从该对象中获得打印结果。

现在,每个扩展base_class的类的引用都是不同的。

我想做的只是将 reference 设置为 extended_class 类的静态属性。

但是,然后将 doSomethingWithReference 更改为 self::$reference 会引发 undefined variable 错误。

相反,在 base_class 中设置静态变量并从 extended_class 修改它不起作用,因为它会更改从该类扩展的所有所有内容的变量。

有什么办法可以做到这一点,这样我就不会从 $this->reference 中打印出来吗?

最佳答案

因为您使用的是 PHP 5.3,所以可以使用 late static binding在运行时解决对正确类的静态调用。

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::$reference 将在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)。

结论:为此,需要在每个需要单个唯一引用的类中声明该属性。请注意,此代码自 5.4.x 起有效。

关于php - 从父类访问子类静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com