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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等