简体   繁体   中英

Setting parent variable from child class

I have a parent class A and a child class B . My goal now is to initialize content (property of A ). Only B knows the value that should go inside A::content .

class A
{
    protected $m;
    protected $n;
    protected $p;
    protected $content;

    public function __construct() {
        $this->init();
        $b = new B;
        $this->content = $b->getContent();
    }

    private function init() {
        $this->m = 'Nobody';
        $this->n = 'is';
        $this->p = 'like';
    }

    public function getContent() {
        return $this->content;
    }
}

class B extends A
{
    protected $x;

    public function __construct() {
        $this->init();
        $this->content = $this->m.' '.$this->n.' '.$this->p.' '.$this->x.' (according to '.$this->x.')';
    }

    private function init() {
        $this->x = 'Donald Trump';
    }
}

$a = new A;
echo $a->getContent();

In my project I have some classes - let's say B,C,D (they look like B in the code above). They all have one thing in common: They need the variables and methods of A to set the value for A::content .

My question now is: Is this a "good" way to achive the goal? An alternative option could be not to use inheritance but to inject an instance of A into the constructor of B ... or maybe other alternatives?

EDIT: Okay... looks like I was not able to make myself clear. Unfortunately my english is not good enough to make my point clearer. I'm new to OOP and I'm not trying to realize all SOLID principles. What I'm trying is to understand
1. where am I?
2. where do I want to get?
3. how can OOP helf me to get there?

EDIT: in my real project I'm instantiating the parent (last two lines of my code). And the parent decides (by parsing $_GET) which child to instantiate : B, C or D

I solved the problem by changing the constructor of B ... thanks to @AP's hint

public function __construct() {
    parent::init();
    $this->init();
    $this->content = $this->m.' '.$this->n.' '.$this->p.' '.$this->x.' (according to '.$this->x.')';
}

Bellow are my corrections to make what you are trying to do work. However you cannot create a A class and expect it to get the data from the B class (which is a child) But you can access the constructor of the A class from the B class

    class A
{
    protected $m;
    protected $n;
    protected $p;
    protected $content;

    public function __construct() {
        $this->init();
    }

    private function init() {
        $this->m = 'Nobody';
        $this->n = 'is';
        $this->p = 'like';
    }

    public function getContent() {
        return $this->content;
    }
}

class B extends A
{
    protected $x;

    public function __construct() {
        parent::__construct();
        $this->init();
        $this->content = $this->m.' '.$this->n.' '.$this->p.' '.$this->x.' (according to '.$this->x.')';
    }

    private function init() {

        $this->x = 'Donald Trump';
    }

     public function getContent() {
        return $this->content;
    }
}

$B = new B;
echo $B->getContent();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM