这不是应该如何编写体面的 OOp。给每个班级自己的文件。据我了解,您有 3 个包含类的文件,并且想要使用实例化对象。使用依赖注入来构造相互依赖的类。
例子:
文件1.php:
class Object
{
public function SomeMethod()
{
// do stuff
}
}
file2.php,使用实例化对象:
class OtherObject
{
private $object;
public function __construct(Object $object)
{
$this->object = $object;
}
// now use any public method on object
public AMethod()
{
$this->object->SomeMethod();
}
}
file3.php,使用多个实例化对象:
class ComplexObject
{
private $object;
private $otherobject;
public function __construct(Object $object, OtherObject $otherobject)
{
$this->object = $object;
$this->otherobject = $otherobject;
}
}
将所有这些结合在一个引导文件或某种程序文件中:
程序.php:
// with no autoloader present:
include_once 'file1.php';
include_once 'file2.php';
include_once 'file3.php';
$object = new Object();
$otherobject = new OtherObject( $object );
$complexobject = new ComplexObject( $object, $otherobject );