【问题标题】:PHP Extending Classes and Sibling AccessPHP 扩展类和同级访问
【发布时间】:2012-06-06 21:58:29
【问题描述】:

好的,所以我正在尝试找出如何最有效地构建我的代码。我最近将它从一个包含我所有方法的巨型类文件转换为与一个基类组合的更小文件。这是我想要的,但是我无法让它正常工作,我需要一些关于结构的帮助。

基本上我需要这样做:

  • 有些函数只是其他类的“工具”(例如转换器函数等) - 它们需要可供所有子类访问。
  • 在某些情况下,“子类 1”需要使用“子类 2”中的函数。
  • 子类需要能够设置另一个子类可以看到的变量。

请告诉我如何开始满足这些要求;代码示例将不胜感激!

也许一些伪代码会有所帮助 - 但如果我不在,请告诉我什么会更好!

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

到目前为止,我尝试过的方法不起作用...请帮助!谢谢。

【问题讨论】:

标签: php class extends siblings


【解决方案1】:

第一点:

对于 Helper 类,您可以选择将方法设为静态,因此您不需要实例:

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

现在可以像这样从任何地方访问:

Helper::usefulMethod()

接下来的几点需要进一步解释,我将其作为 cmets 添加到您的伪代码中:

// 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();
    }
}

我确信这并不能完全回答所有问题,但我已尽力而为。 也许你可以进一步提出你的意图

【讨论】:

  • 我会投票,但我没有足够的代表,谢谢你的帮助!这是一个很好的开始。不过,我应该补充一点,我想我的意思是我想从子类对基类中的受保护变量进行更改。更准确地说,我想echo('private call: '.parent::$test)。这行得通吗?
  • 另外,如果 Child1 有 Child2 需要使用的方法呢?我怎样才能访问它?
  • 调用$this->test就够了,如果是从主类派生的,因为属性合并到基类中。如果您想从 Child2 访问一个方法,您可以实例化一个新对象并将其提供给另一个类。叫dependecy injection。如果您需要帮助,请说一句话:)
  • 是的,我想我明白了 - 基本上只是将 $child2 = new Child2 放入需要 Child2 的 Child1 方法中并以这种方式引用它?没关系,我只是认为有一种更好的方法,而不是每次我需要某些东西时都不必创建新的引用。谢谢丹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 2014-01-07
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多