PHP 扩展类和同级访问

标签 php class extends siblings

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

基本上我需要这样做:

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

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

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

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

到目前为止我所尝试的方法都不起作用...请帮忙!谢谢。

最佳答案

第一点:

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

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

现在可以从任何地方访问它,如下所示:

Helper::usefulMethod()

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

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

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

关于PHP 扩展类和同级访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922907/

相关文章:

c# - 创建在 DLL 内部公开但在外部不可访问的 C# 类

php - 扩展时找不到类

java - "extends"类的层次结构

php - Lumen - 更改默认存储路径

php - 将 pubdate 或 timestamp 插入数据库

php - PHP/MySQL 中的原子有界增加

python - 尝试同步数据库时出现类错误(Python Django)

使用类的 Python 前 20 个名称代码

Java通过匿名类强制第三方类上的接口(interface)

PHP:为什么无法识别类中的变量 PHPSESSID?