Php 从嵌套类访问父作用域

Php 从嵌套类访问父作用域,php,class,inheritance,Php,Class,Inheritance,我有一门主课 class MY_API { function __construct($db) { $this->something = new MY_SOMETHING($db); $this->anotherthing = new MY_ANOTHERTHING($db); } } 这样我就可以用 $this->something->sometfunction() 但我不确定如何访问: $this->anotherthing

我有一门主课

class MY_API {
    function __construct($db) {
        $this->something = new MY_SOMETHING($db);
        $this->anotherthing = new MY_ANOTHERTHING($db);
    }
}
这样我就可以用
$this->something->sometfunction()

但我不确定如何访问:

$this->anotherthing->anotherfunction();
从内部:

$this->something->somefunction();
我想我需要这样的东西:

$this->parent->anotherthing->anotherfunction();
这是可能的,还是我需要改变我构建类的方式


理想情况下,我只希望这些函数位于不同的文件中,而不是拥有一个非常大的文件,并且每个文件中的每个函数都可以相互访问

如果
MY\u SOMETHING
依赖于
MY\u ANOTHERTHING
,请插入它

class MY_SOMETHING {
  private $db;
  private $anotherThing;

  public function __construct($db, MY_ANOTHERTHING $anotherThing) {
    $this->db = $db;
    $this->anotherThing = $anotherThing;
  }
在您的
MY_API
构造函数中

public function __construct($db) {
    $this->anotherthing = new MY_ANOTHERTHING($db);
    $this->something = new MY_SOMETHING($db, $this->anotherThing);
}

现在,您的
MY\u SOMETHING
类可以在其任何方法中使用
$this->anotherThing

好的,这很有意义,如果它们都需要相互访问函数呢?理想情况下,我希望所有类都能访问所有其他类,这样我就可以轻松地组织我的代码。这听起来不像是组织,更像是一团混乱。也许你不想使用OOP,而是想要一堆函数(全局函数或名称空间函数)谢谢Phil,当所有函数都位于主类中时,它就可以工作了,但是我只是尝试在不同的文件之间分解代码,从而将一些函数拆分为它们自己的类,然而,这使我无法引用每个子类函数,扩展类似乎不是一个好的解决方案,也许我只需要处理一个很长的类文件。我建议您阅读关于OOP的内容,特别关注S~“单一责任原则-一个班级应该只有一项责任”