本文介绍了PHP父函数无法看到子类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的抽象父类拥有一个可以被子类继承的方法,该方法将允许该子类遍历其所有变量(包括从父类继承的变量,以及它们自己的变量). /p>

目前,如果我在父对象中实现此方法,那么仅父对象的变量将被迭代:

class MyObject {

    private $one;
    private $two;
    private $three;

    function assignToMembers() {
        $xx = 1;
        foreach($this as $key => $value) {
            echo "key: ".$key."<br />";
            $this->$key = $xx;
            $xx++;
        }
    }

    public function getOne() {
        return $this->one;
    }

    public function getTwo() {
        return $this->two;
    }

    public function getThree() {
        return $this->three;
    }
}


class MyObjectSubclass extends MyObject {

    private $four;
    private $five;

    public function getFour() {
        return $this->four;
    }

    public function getFive() {
        return $this->five;
    }
}
$o = new MyObjectSubclass();
$o->assignToMembers();
echo $o->getOne()." ";
echo $o->getTwo()." ";
echo $o->getThree()." ";
echo $o->getFour()." ";
echo $o->getFive()." ";

// This prints 1 2 3

另一方面,如果我将assignToMembers函数放在子类中,则仅迭代子类的成员.

因为我希望我的assignToMembers()函数可以被许多子类使用,所以我不想只在父类中就不必在每个子类中都实现它,但是看起来我将必须这样做,除非它可以访问该班级的成员.

有没有办法做到这一点?

谢谢.

解决方案

如果您希望代码按所述方式工作,则需要使用protected.记住访问/可见性修改器的作用:

  • private-仅限班级访问
  • protected-整个继承链访问
  • public-通用访问权限

I would like to have my abstract parent class have a method that would be inherited by a subclass which would allow that subclass to iterate through all of it's variables (both the variables inherited from the parent, and it's own variables).

At the moment if I implement this method in the parent, then only the parent's variables will be iterated over:

class MyObject {

    private $one;
    private $two;
    private $three;

    function assignToMembers() {
        $xx = 1;
        foreach($this as $key => $value) {
            echo "key: ".$key."<br />";
            $this->$key = $xx;
            $xx++;
        }
    }

    public function getOne() {
        return $this->one;
    }

    public function getTwo() {
        return $this->two;
    }

    public function getThree() {
        return $this->three;
    }
}


class MyObjectSubclass extends MyObject {

    private $four;
    private $five;

    public function getFour() {
        return $this->four;
    }

    public function getFive() {
        return $this->five;
    }
}
$o = new MyObjectSubclass();
$o->assignToMembers();
echo $o->getOne()." ";
echo $o->getTwo()." ";
echo $o->getThree()." ";
echo $o->getFour()." ";
echo $o->getFive()." ";

// This prints 1 2 3

On the other hand, if I put the assignToMembers function in the subclass, then only the subclass's members are iterated over.

Because I want my assignToMembers() function to be usable by a number of subclasses, I don't want to have to implement it in every one, only in the parent class, but it looks like I will have to unless it can access that class's members.

Is there any way to acheive this?

Thanks in advance.

解决方案

You'll need to use protected if you want your code to work as described. Remember the roles of the access/visibility modifiers:

  • private - class level only access
  • protected - whole inheritance chain access
  • public - universal access

这篇关于PHP父函数无法看到子类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:30