从父类访问子类静态变量? [英] Access child class static variables from parent class?

查看:47
本文介绍了从父类访问子类静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

很简单,

class base_class {公共函数 doSomethingWithReference(){$this->reference->doSomething();}}类扩展类扩展基类{受保护的 $reference;公共函数 __construct($ref){$this->reference = $ref;}}

现在这显然工作正常,

但是,当我调试时,我并不关心 $this->reference

的值

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

因此,当我执行 print_r($instanceOfExtendedClass) 时,我会打印出该对象.

现在每个扩展 base_class 的类的引用都不同.

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

但是,然后将 doSomethingWithReference 更改为 self::$reference 会引发未定义的变量错误.

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

有没有办法做到这一点,这样我就不会打印出$this->reference?

解决方案

因为您使用的是 PHP 5.3,所以您可以使用 后期静态绑定,在运行时解析对正确类的静态调用.

class base_class {公共函数 doSomethingWithReference(){静态::$reference->doSomething();}}类扩展类扩展基类{受保护的静态 $reference;公共函数 __construct($ref){静态::$reference = $ref;}}

重要提示:extended_class::$reference 将在 extended_classall 之间共享代码>实例.如果这不是您的意图,这将行不通.

您似乎实际上担心内存或资源使用.在 PHP 中,所有对象都是通过引用传递的.这意味着将对象作为参数传递或创建它的副本等不会消耗额外的内存.如果您需要在许多其他对象中引用一个对象,这样做不会消耗额外的内存.

<小时><块引用>

如果我有extended_class 和另一个相同的类(比如extended_class1),它们是否也会共享引用?或者所有extended_class'实例共享一个引用,而所有extended_class1'实例共享另一个(理想情况)?

看起来共享是基于静态变量的定义位置.两个示例,均来自 PHP 交互式提示:

php >class Shared { public $me;公共函数 __construct($me) { $this->me = $me;} }php >class Base { protected static $ref;公共函数 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 >类 Inherit_3 扩展了 Inherit_1 {}php >$shared_1 = 新共享(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();3php >$in_2-> foo();3php >$in_3-> foo();3

在这种情况下,因为引用存在于基类中,所以每个人看到的都是相同的.我想这有点道理.

当我们声明每个子类的引用时会发生什么......大部分时间?

php >class Shared { public $me;公共函数 __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;公共函数 __construct($ref) { static::$ref = $ref;} }php >class Inherit_2 extends Base { protected static $ref;公共函数 __construct($ref) { static::$ref = $ref;} }php >类 Inherit_3 扩展了 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();3php >$in_2-> foo();2php >$in_3-> foo();3php >$in_4->foo();4

因为 3 继承自 1 而没有声明它自己的静态属性,所以它继承了 1.当我们将 3 设置为 Shared(3) 时,它会覆盖 1 现有的 Shared(1).

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

I have a base class that I need to call functions on a class that is referenced in the child class.

Easy enough,

class base_class {

    public function doSomethingWithReference(){
        $this->reference->doSomething();
    }
}

class extended_class extends base_class{

    protected $reference;

    public function __construct($ref){
        $this->reference = $ref;
    }
}

Now this works fine obviously,

But, when I am debugging I don't care about the value of $this->reference

But, the object that $this->reference refers to is huge!

so, when I do print_r($instanceOfExtendedClass) I get the print out of that object.

Now the reference is different for each class that extends base_class.

What I wanted to do was just set reference as a static property on the extended_class class.

But, then changing doSomethingWithReference to be self::$reference throws an undefined variable error.

And conversely setting the static variable in base_class and modifying it from extended_class doesn't work as it changes the variable for all everything that extends from that class.

Is there any way to do this so I don't get the print out of $this->reference?

解决方案

Because you are using PHP 5.3, you can use late static binding to resolve the static call to the right class at runtime.

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;
    }

}

Big fat reminder: That one extended_class::$reference is going to be shared amongst all of the extended_class instances. If that is not what you intend, this is not going to work.

You seem to actually be worried about memory or resource use. In PHP, all objects are passed by reference. This means that passing an object as an argument, or creating a copy of it, etc, does not consume extra memory. If you need to reference an object in a number of other objects, doing so will not consume extra memory.


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)?

It looks like the sharing is based on where the static variable is defined. Two examples, both from the PHP interactive prompt:

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

In this case, because the reference lives in the base class, everyone sees the same one. I suppose this makes some sort of sense.

What happens when we declare the reference with each child class.. most of the time?

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

Because 3 inherited from 1 without declaring it's own static property, it inherited 1's. When we set 3's to Shared(3), it overwrote 1's existing Shared(1).

Conclusion: For this to work, the property needs to be declared in every class that needs the single unique reference. Note that this code is valid as of 5.4.x.

这篇关于从父类访问子类静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆