Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从子类设置父变量_Php_Oop_Inheritance_Dependency Injection - Fatal编程技术网

Php 从子类设置父变量

Php 从子类设置父变量,php,oop,inheritance,dependency-injection,Php,Oop,Inheritance,Dependency Injection,我有一个父类a和一个子类B。我现在的目标是初始化内容(A的属性)。只有B知道应该放在A::content中的值 class A { protected $m; protected $n; protected $p; protected $content; public function __construct() { $this->init(); $b = new B; $this->conte

我有一个父类
a
和一个子类
B
。我现在的目标是初始化
内容
A
的属性)。只有
B
知道应该放在
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();
在我的项目中,我有一些类——比如B、C、D(它们看起来像上面代码中的
B
)。它们都有一个共同点:它们需要
A
的变量和方法来设置
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();
我现在的问题是:这是实现目标的“好”方法吗?另一种选择是不使用继承,而是将
A
的实例注入
B
的构造函数中。。。或者其他选择

编辑: 可以看来我说不清楚。不幸的是,我的英语不够好,不能使我的观点更清楚。 我是OOP新手,我不想实现所有的S.O.L.I.D。原则。我想了解的是
1.我在哪里?
2.我想去哪里?
3.OOP怎么能帮我到那里

编辑: 在我的实际项目中,我正在实例化父级(代码的最后两行)。父节点决定(通过解析$\u GET)实例化哪个子节点:B、C或D

我通过更改
B
的构造函数解决了这个问题。。。感谢@A.P.的提示

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

下面是我的更正,以使你正在试图做的工作。但是,您不能创建一个a类并期望它从B类(子类)获取数据 但您可以从B类访问A类的构造函数

    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();

A(父母)不应该知道B(孩子)。你正在扭转这种关系。A是独立的,不需要子类。B是依赖的,需要父类。在您的设计中,子类是独立的,父类是依赖的。我认为您应该研究抽象类,以便为所有类定义相同的方法,您是否已经探索过这种方法?您不应该使用new In-Constructor,因为它是隐藏的依赖项。但我不确定你想在这里完成什么。你能随意更改A和B的代码吗?例如,将init()方法的可见性更改为public/protected?您的示例有效。。。使用父构造函数是一个很好的提示。Thnx。我没有提到的是:在我的实际项目中,我正在实例化A。A决定(通过解析$u GET)实例化哪个子级:B、C或D。我不能实例化B(代码的最后两行),因为我不知道我需要的是B、C还是D。“你不能创建一个A类,并期望它从B类中获取数据”-我可以,我会;-)