3
我正在PHP中構建一個框架。 我在library/core.php中有一個導入函數。如何使用分配給另一個類的屬性的類的實例php
我可以使用這樣的功能:
$core->import("someclass");
這是函數:
public function import()
{
$import_resources = func_get_args();
$check_directories = array("library", "template", "view", "action", "errors");
$instances = array();
foreach($import_resources as $resource)
{
for($i = 0; $i <= count($check_directories) - 1; $i++)
{
if(file_exists($this->appRoot() . $check_directories[$i] . "/" . $resource . ".php"))
{
$classes = get_declared_classes();
include ($check_directories[$i] . "/" . $resource . ".php");
$included_classes = array_diff(get_declared_classes(), $classes);
$last_class = end($included_classes);
$last_class_lowercase = strtolower($last_class);
$this->$last_class_lowercase = new $last_class();
// create an instance of the included class and attach it to the Core Class
}
else
{
}
}
}
}
所以在其他類,我可以使用它像這樣:
$core->import("view");
$core->view->get();
這一點的全部意義在於,當擴展時,使包含的類在另一個類中可用。
class Someclass extends Core
{
public function somefunc()
{
$this->view->get(); // This does not work.
}
}
我怎麼能使它像這樣工作?這是框架的一個非常重要的部分,因爲它是如何工作的。我認爲它在CodeIgniter等流行框架中的工作原理也類似。
我試圖使用parent::view->get()
,但我想我不完全理解它。
我希望我能弄明白這一點,因爲它讓我在工作中失望。 預先感謝您。
什麼是「核心」,反正呢?它是一個進口商嗎?那麼也許你應該把它稱爲「進口商」。 –
爲什麼不使用自動加載?更好的是,遵循PSR-0並使用標準自動裝載器。 –
@Waleed Khan;不,不,核心是一個核心課程,具有所有必要的功能。它應該包含在我的框架中的每個php文件中。 Core導入功能只是導入文件的一種更簡單的方法。也許我可以在以後重新命名類,但現在我必須專注於使框架工作。 – Rasteril