Access child class static variables from parent class?

后端 未结 2 562
心在旅途
心在旅途 2021-02-05 13:42

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          


        
相关标签:
2条回答
  • 2021-02-05 14:26

    Provide a way for the parent to access the child's static member:

    <?php
    
    abstract class base_class {
        abstract protected function reference();
        // If you want to be able to instanciate base class directly, use the following 2 lines in place of the previous 2 lines:
    //class base_class {
        //protected function reference() {}
    
        public function doSomethingWithReference(){
            $this->reference()->doSomething();
        }
    }
    
    class extended_class extends base_class{
        protected static $reference;
    
        public function __construct($ref){
            self::$reference = $ref;
        }
    
        protected function reference(){
            return self::$reference;
        }
    }
    
    class ref {
        public function doSomething(){
            echo __METHOD__;
        }
    }
    
    $ref = new ref();
    $ec = new extended_class($ref);
    $ec->doSomethingWithReference();
    
    print_r($ec);
    ?>
    
    0 讨论(0)
  • 2021-02-05 14:30

    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.

    0 讨论(0)
提交回复
热议问题