2012-06-06 53 views
2

好了,所以我想弄清楚如何最有效地組織我的代碼。我最近用我所有的方法將它從一個巨型類文件轉換爲與一個基類相結合的更小的文件。這是我想要的,但是我無法讓它正常工作,我需要一些結構幫助。PHP擴展類和同胞訪問

基本上我需要這樣做:

  • 會有一些功能,這些功能只是「工具」,爲其他類(如轉換器功能等) - 他們需要的是所有的訪問兒童班。
  • 在某些情況下,「子類一個」將需要使用的功能從「子兩課」。
  • 一個子類需要能夠設置另一個子類可以看到的變量。

請讓我知道我可以開始使用這些要求;代碼示例將不勝感激!

或許有些僞代碼將幫助 - 但如果我走了,請讓我知道什麼會更好地工作!

class main { 
    private $test; 
    public function __construct() { 
     //do some stuff 
    } 
    public function getTest() { 
     echo('public call: '.$this->test); 
    } 
    private function randomFunc(){ 
     echo('hello'); 
    } 
} 
class child1 extends main { 
    public function __construct() { 
     parent::$test = 'child1 here'; 
    } 
} 
class child2 extends main { 
    public function __construct() { 
     echo('private call: '.parent::$test); //i want this to say "private call: child1 here" 
     parent::randomFunc(); 
    } 
} 
$base = new main; 
new child1; 
new child2; 
$base->getTest(); 

所以我想的,其結果是:

private call: child1 here 
hello 
public call: child1 here 

到目前爲止,我已經試過不行......請幫助!謝謝。

+0

你看過使用特質 - http://php.net/manual/en/language.oop5.traits.php –

回答

2

第一點:

針對助手類,你可以選擇讓你的方法靜態的,所以你並不需要一個實例:

class Helper 
{ 
    public static function usefulMethod() 
    { 
    // do something useful here 
    } 
} 

也就是說現在可以訪問來自世界各地這樣的:

Helper::usefulMethod() 

下一個點需要一些進一步的解釋,這是我添加評論你r僞代碼:

// always start your class names with upper case letter, it's a good convention 
class Main 
{ 
    // don't make this private if you want to access from the child classes too 
    // private $test; 

    // instead make it protected, so all sub classes can derive 
    protected $test; 

    public function __construct() 
    { 
     //do some stuff 
    } 

    public function getTest() 
    { 
     echo 'public call: '.$this->test; 
    } 

    private function randomFunc() 
    { 
     echo 'hello'; 
    } 
} 

class Child1 extends Main 
{ 
    public function __construct() 
    { 
     // $test is now derived from the parent into the sub class 
     $this->test = 'child1 here'; 
    } 
} 


class Child2 extends Main 
{ 
    public function __construct() 
    { 
     // this doesn't quite work like expected: 
     // echo 'private call: '.$this->test; //i want this to say "private call: child1 here" 

     // because this isn't a reference, but think of every class instance 
     // as a container which holds its own properties and methods (variables and functions) 
     // if you really want to do it like intended then you would need a subclass from main1 
     // but that is kind of strange, I don't know exactly what you want to achieve 

     // this will print out 'hello' as expected 
     parent::randomFunc(); 
    } 
} 

我相信這並不能完全回答所有問題,但我盡力了。 也許你可以進一步提出你的意圖

+0

我會投這個,但我不有足夠的代表,謝謝你的幫助!這是一個很好的開始。我應該加上,但我想我的觀點是我想從子類中更改基類內的受保護變量。更確切地說,我想'echo('private call:'.parent :: $ test)'。這會起作用嗎? – iLoch

+0

另外,如果Child1有一個Child2需要使用的方法呢?我怎樣才能訪問它? – iLoch

+0

當您調用$ this-> test時,如果它是從主類派生的,因爲該屬性被合併到基類中,就足夠了。如果你想從Child2訪問一個方法,你可以創建一個新的對象並把它放到另一個類中。稱爲[依賴注入](http://fabien.potencier.org/article/11/what-is-dependency-injection)。如果您需要提供幫助的,只說了一句話:) –