【问题标题】:Properties shared between child and parents class in phpphp中子类和父类之间共享的属性
【发布时间】:2013-07-25 15:03:21
【问题描述】:
class parents{
    public $a;
    function __construct(){
        echo $this->a;
    }
}
class child extends parents{
   function __construct(){
        $this->a = 1;
        parent::__construct();
    }
}
$new = new child();//print 1

上面这段代码打印1,这意味着每当我们创建一个子类的实例,并为从其父类继承的属性赋值时,其父类中的属性也已被赋值。但下面的代码显示不同:

class parents{
    public $a;
    function test(){
        $child = new child();
        echo $this->a;
    }
}

class child extends parents{
    function __construct(){
        $this->a = 1;
    }
}
$new = new parents();
$new->test();//print nothing

我给它的子类赋值,而父类显然没有赋值给它的子类的值,为什么?

谢谢!

【问题讨论】:

    标签: php constructor inheritance


    【解决方案1】:

    这是因为在第一个例子中你实例化了孩子

    $new = new child();//print 1
    

    然后你在第二个实例化父母

    $new = new parents();
    

    第二个与第一个相同,使用 $new = new child();

    如果你想通过实例化 child() 来访问 $a,你需要这样做:

    class parents{
        public $a;
        function test(){
            $child = new child(); //CHANGE HERE
            echo $child->a;
        }
    }
    
    class child extends parents{
        function __construct(){
            $this->a = 1;
        }
    }
    $new = new parents();
    $new->test();
    

    【讨论】:

      【解决方案2】:

      当您实例化parent 时,创建的child 实例将parent 类扩展为test() 函数。但是,这不会改变 $a 的值。

      【讨论】:

        【解决方案3】:

        发生这种情况是因为您有两个不同的实例,它们没有任何共同点(除了继承......) 除了可以使用内部类生成之外的行为 - php 不支持。

        如果你想在每个实例上共享一个 var accros,你必须将其设为静态

        class parents{
            public static $a;
            function test(){
                $child = new child();
                echo self::$a;
            }
        }
        
        class child extends parents{
            function __construct(){
                self::$a = 1;
            }
        }
        $new = new parents();
        $new->test();
        

        这可能不是您想要的。或者你确切地说,你想在哪里改变你的 var

        class parents{
            public $a;
            function test(){
                $child = new child();
                echo $child->a;
            }
        }
        
        class child extends parents{
            function __construct(){
                $this->a = 1;
            }
        }
        $new = new parents();
        $new->test();
        

        【讨论】:

          【解决方案4】:

          在上面的例子中,由于构造函数是从子类中调用的,所以它把正在使用的对象当作只是使用父类中的函数的子对象,就好像它是自己的一样.

          在下面的示例中,您有两个独立的对象独立运行。父对象有 $a,子对象也有,但它们不是同一个 $a,因为它们包含在不同的对象中。因此,当您在父类中打印 $this->a 时,它指的是父类的 $a 实例,而如果在子类中设置 $this->a =1 后回显 $a ,它将显示子类的实例$a。

          希望这为您解决了一些问题。

          【讨论】:

            【解决方案5】:

            您正在混合对象组合和类继承。

            继承(通过 extends 关键字实现)定义is a 关系。

            Composition 定义了has a 关系。

            为了说明这个概念,我们将从继承开始。

            class Person {
                public $name;
                public function talk(){};
                public function poop(){};
            }
            
            class Parent extends Person {
                public function __construct($name) {
                    $this->name = $name;
                }
            }
            
            class Child extends Person {
                public function __construct($name){
                    $this->name = $name;
                }
            }
            

            在这个例子中,我们定义了一个class 的东西,叫做 People。根据该定义,我们派生出两种不同的人子类型,即父母和子女。当我们对一个类进行子类型化时,子类型会获得它自己的所有属性的副本,并且可以访问基类型中定义的所有方法,因此如果没有定义它,子类型和父类型就有名称,并且可以说话和拉屎也是一个人。

            例如:

            $parent = new Parent("Homer");
            $child = new Child("Bart");
            $parent->talk();
            $child->poop();
            

            当你想实现一个有关系时使用组合。让我们修改一下 Parent 的类型定义。

            class Parent extends Person {
                public $children = array();
            
                public function __construct($name) {
                    $this->name = $name;
                }
                public function addChild(Child $child){
                    $this->children[] = $child;
                }
            }
            

            我们现在允许 have a 子级的父级。

            $parent = new Parent("Homer");
            $child = new Child("Bart");
            $parent->addChild($child);
            // now I can access homers first child
            echo $parent->children[0]->name;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-10-26
              • 1970-01-01
              • 2015-03-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-05-11
              相关资源
              最近更新 更多