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