duan1933 2018-02-05 18:06
浏览 59
已采纳

访问父母父类

I have the class structure below. I was wondering how I can access $varA from class C. And if I change a value within class A is it also visible in class C? Or when I call class C, are all the values at that moment transferred to the newly-created class?

class A {
  protected $varA;
  protected function functionA() {
    echo 'hello';
  }
}

class B extends A {
  protected $varB;
  protected function functionB() {
    echo 'world';
  }
}

class C extends B {
  protected $varC;
  protected function functionC() {
    echo 'Class C';
  }
}
  • 写回答

2条回答 默认 最新

  • dongxi1965 2018-02-05 18:50
    关注

    Based on your questions, it sounds like you may be misunderstanding the distinction between classes and objects. Think of the class as a specification from which objects are created. You aren't really going to be manipulating classes in your code, you're going to be creating objects from those classes and working with those instances of the classes. Classes like the ones you've shown aren't really anything until they're instantiated.

    When you create an instance of class C with $c = new C;, the resulting object $c will have access to all of the public and protected properties of any classes it inherits from; in this case A and B. Those properties are accessible anywhere in the C class. You can access them with $this->varA, for example.

    But the way you have it written, none of the classes have any public properties or methods. So any code you write that uses them isn't going to have access to any of the properties. protected means that the property or method will only be available within the class or classes that inherit from it. So after $c = new C;, you won't be able to call $c->functionC(), and you won't be able to refer to $->varA, or even $c->varC.

    If you create an instance of A and an instance of C, those two objects are completely independent of each other. The instance of C has its own $varA, and if you change the $varA in the instance of A, it will not affect the instance of C.

    Here are a couple of example classes with public methods to demonstrate this.

    class A {
        protected $varA = 'a';
        public function changeA ($newValue) {
            $this->varA = $newValue;
        }
    }
    
    class B extends A {
        public function example()
        {
            echo $this->varA;
        }
    }
    

    If you create a new B, its example method will show the inherited value in $varA.

    $instanceOfB = new B;
    $instanceOfB->example();  // outputs a
    

    Then if you create a new A, and change the value of its $varA...

    $instanceOfA = new A;
    $instanceOfA->changeA('something else');  // varA in this object is modified
    

    The value of $varA in the instance of B is unaffected.

    $instanceOfB->example();  // still outputs a
    

    I just used one level of inheritance in the example, but the concept is the same with a A/B/C setup you showed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景