2012-06-06 56 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)。如果您需要提供帮助的,只说了一句话:) –