好了,所以我想弄清楚如何最有效地组织我的代码。我最近用我所有的方法将它从一个巨型类文件转换为与一个基类相结合的更小的文件。这是我想要的,但是我无法让它正常工作,我需要一些结构帮助。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
到目前为止,我已经试过不行......请帮助!谢谢。
你看过使用特质 - http://php.net/manual/en/language.oop5.traits.php –