如何从子类访问父类?

时间:2016-06-30 19:11:25

标签: php class

我正在尝试自动化表单验证和数据库插入过程。在特定部分,我需要能够从子类访问父类。确切地说,我需要访问父类来访问另一个子类。

我试图复制我在实际代码中使用的相同结构:

class Family {
    public function __construct($members = []) {
        foreach ($members as $member) {
            $this->$member[0] = new Member($member);
        }
    }
}
class Member {
    public $name;
    public $niisan;
    public function __construct($name = []) {
        $this->name = $name[0];
        if (count($name) > 1) {
            $this->get_niisan($name[1]);
        }
    }
    public function get_niisan($member_lookup) {
        $this->niisan = $this->name . " has a niisan named X";
    }
}

$Winchesters = new Family([
    ["Dean"],
    ["Sam", "Dean"],
]);

输出:

object(Family)#1 (2) {
    ["Dean"]    => object(Member)#2 (2) {
        ["name"]    => "Dean"
        ["niisan"]  => NULL
    }
    ["Sam"]     => object(Member)#3 (2) {
        ["name"]    => "Sam"
        ["niisan"]  => "Sam has a niisan named X"
    }
}

我想要做的是在主类中创建两个子类。创建子类的顺序很重要。 ["Sam"]取决于["Dean"],因此需要首先创建["Dean"]["Dean"]中的某些属性将在["Sam"]中使用。

在JavaScript中,我会这样写:

function Family(members) {
    var parent = this; // Family object
    for (var i = 0; i < members.length; i++) {
        // passing a reference of Family to the new Member object
        // so that it can access the main object later
        this[members[i][0]] = new Member(parent, members[i]);
    }
}

function Member(parent, name) {
    // saving the reference to the parent object
    this.parent = parent;
    this.name = name[0];
    this.niisan = "";
    this.get_niisan = function (lookup) {
        // accessing the parent object and then getting the name property of "Dean" object
        this.niisan = this.name + " has a niisan named " + this.parent[lookup].name;
    };
    if (name.length > 1) {
        this.get_niisan(name[1]);
    }
}

var Winchesters = new Family([
    ["Dean"],
    ["Sam", "Dean"]
]);

将主对象传递给每个子对象。 PHP必须有自己的方式来处理这个问题。我只是不知道。

更新:对于向下/关闭选民:这怎么不清楚?

我有两个班:家庭和会员。成员类在Family类中初始化。以下代码

$Winchesters = new Family([
    ["Dean"],
    ["Sam", "Dean"],
]);

结果:

object(Family)#1 (2) {
    ["Dean"]    => object(Member)#2 (2) {
        ["name"]    => "Dean"
        ["niisan"]  => NULL
    }
    ["Sam"]     => object(Member)#3 (2) {
        ["name"]    => "Sam"
        ["niisan"]  => "Sam has a niisan named X"
    }
}

在运行时,首先创建一个名为“Dean”的Member,并将其分配给Dean内的变量Family。 其次,Member“Sam”已创建,但Sam依赖于第一个创建的对象DeanSam需要访问父类Family,然后访问Dean,并获取属性name并在分配niisan变量时使用它。

请查看JavaScript示例。我想模仿PHP中的确切功能。将主类传递给子类,以便子类可以访问主类中后来创建的变量。

2 个答案:

答案 0 :(得分:3)

  

在特定部分,我需要能够访问父类   来自儿童班。确切地说,我需要访问父类   访问另一个子类。

您无法从父类访问另一个子类。只有孩子才能访问父母。在您的示例中,您尚未创建任何类型的继承。这就是你在PHP中创建继承的方法。

class FooParent {
    public function display($text) {
        echo $text;
    }
}

class Foo extends FooParent {
    public function someMethod() {

        // ask the parent to display the text
        parent::display("Hello World!");
    }
}

$foo = new Foo();
$foo->someMethod();

哪个会显示“Hello World!”。希望这将有助于您入门,了解有关PHP的OOP版本的更多信息 - http://php.net/manual/en/language.oop5.php

答案 1 :(得分:2)

您的术语令人困惑。在PHP OOP中, parent child 通常是指彼此继承的类之间的关系,而不是那些类的对象(C ++称之为 base 派生类,Javascript使用父类的原型链。

您需要做的是在Family课程中存储对Member的引用。

class Member {
    public $name;
    public $niisan;
    public $family;

    public function __construct($name = [], $family) {
        $this->family = $family;
        $this->name = $name[0];
        if (count($name) > 1) {
            $this->get_niisan($name[1]);
        }
    }
    public function get_niisan($member_lookup) {
        $this->niisan = $this->name . " has a niisan named X";
    }
}

class Family {
    public function __construct($members = []) {
        foreach ($members as $member) {
            $this->$member[0] = new Member($member, $this);
        }
    }
}

然后,如果Member类想要访问兄弟姐妹,它可以使用$this->family来获取包含的家庭。

BTW,在Family中使用变量命名的属性是糟糕的设计,恕我直言。相反,您应该使用包含关联数组的单个$member属性:

$this->members = array();
foreach ($members as $member) {
    $this->members[$member[0]] = new Member($member, $this);
}