0

我想知道在构造函数中调用一个类来访问该类的某些方法是否正确:

class myClass {

    private static $instance;   
    private $header;

    private function __construct() {

       $callAnotherClass = new callAnotherClass();
       $someVariable = $callAnotherClass->someMethod( 'param' );

    }

}

我自愿不发布构造函数的内容。

4

1 回答 1

1

没有什么错,只是做类似的事情是不正确的

$random = new Random();

这当然是 100% 有效的。

在这种情况下

callAnotherClass() 

恰好是您的自制课程之一,而不是内置课程。

只要没有依赖关系被破坏并且您拥有访问 callAnotherClass 的所有权限,那么您就可以了。

于 2013-03-08T02:56:21.453 回答