2013-07-25 111 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;