我想知道這樣的組合是否可能。從類A調用類B並繼承類A的屬性?
classA.php
class A
{
public $one = "one";
function start() {
$this->one = "seven";
include "classB.php";
$two = new B;
print_r(get_defined_vars());
}
}
classB.php
class B extends A
{
public $two = "two";
function __construct() {
echo($this->one);
}
}
最後的index.php
include "classA.php";
$A = new A;
$A->start();
當我運行的index.php,我得到如下:
Array
(
[one] => seven
[B] => B Object
(
[two] => two
[one] => one
)
)
有沒有辦法讓B類使用更新的變量?或者我必須將代碼分成兩個文件並單獨使用?這樣它至少工作...但我不喜歡我的代碼在課堂以外,我儘量減少使用全球範圍...
BTW:我知道__construct()的,我寫的代碼不__construct使__construct()根本不會被調用。 – 2014-09-23 22:51:54
你真的應該多讀一點關於繼承的實際工作....如果B類擴展了A類,那麼你應該實例化B類......但是不能通過包含類B中的類B嵌入代碼....那根本不是遺傳 – 2014-09-23 22:53:12
你想達到什麼目的? – sectus 2014-09-23 22:53:16