這不是多麼體面的OOp應該編程。爲每個課程分配自己的文件。據我瞭解,你有3個文件在其中的類,並希望使用實例化的對象。使用依賴注入來構造相互依賴的類。
實施例:
file1.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;
}
}
領帶這都聚集在一個引導文件或某種程序文件:
program.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);
這也不是很基本的,面向對象不應該這樣 – JvdBerg
@JvdBerg進行編程,我已經編輯我的職務,因此可以更清晰 – user1611830
邊注:這不要緊,你是否有在不同的文件或不類 - 文件不是範圍/可見性邊界。 – VolkerK