2013-07-25 63 views
-1
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,这意味着当我们创建一个子类的实例,并且将值分配给从其父,在它的父类的属性也继承属性一直assigned.But下面的代码显示了不同:属性共享

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 

哪里值分配给它的子类和父apprently没有它分配给它的子类中的价值,为什么呢?

谢谢!

回答

0

在上面例子中,由于构造函数被从子类中调用,它是治疗对象中使用,就好像它是子对象那就是在父类中使用一个函数,就好像它是自己的函数一样。

在底部示例中,您有两个单独的独立对象。父对象具有$ a,孩子也是这样,但它们不是相同的$ a,因为它们包含在单独的对象中。所以当你在父类中打印$ this-> a时,它指的是父类的$ a实例,而如果在子类中设置$ this-> a = 1后回显$ a,它将显示子类的实例美元。

希望这清除了一些东西给你。

0

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

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

,并在第二个实例化父母

$new = new parents(); 

第二个作品一样先使用$新=新的儿童() ;

如果你想通过实例孩子访问$ 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(); 
0

当你实例parentchild创建的实例parent类到test()功能的延伸。但是,这并不会改变$a的值。

0

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

如果你想分享一个变种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(); 
0

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

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

组合定义了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; 
    } 
} 

在这个例子中,我们正在定义名为People的东西class。从这个定义中,我们得出两种不同的人,父母和孩子的亚型。当我们为一个类子类型时,子类型获得它自己的所有属性的副本,并且可以访问在基类型中定义的所有方法,所以如果没有定义它,Child和Parent就会有一个名称,并且可以通过德也是一个人。

例如:当你想实现一个有关系的船

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

组合物中使用。让我们修改父类型的定义。

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;