如何在php中使用分配给另一个类中另一个类的属性的类的实例

如何在php中使用分配给另一个类中另一个类的属性的类的实例,php,Php,我正在用PHP制作一个框架。 我在library/core.php中有一个导入函数 我可以使用如下功能: $core->import("someclass"); $core->import("view"); $core->view->get(); <?php // --- Begin Importer.php -------------------- class Importer{ protected $classes = array(); p

我正在用PHP制作一个框架。 我在library/core.php中有一个导入函数

我可以使用如下功能:

$core->import("someclass");
$core->import("view");
$core->view->get();
<?php
// --- Begin Importer.php --------------------
class Importer{
    protected $classes = array();

    public function __get($method_name){
        if(array_key_exists($method_name, $this->classes)){
            return $this->classes[$method_name];
        }
    }

    public function import($class_name){
        // Do this or use an auto loader
        require_once __DIR__ . "/../classes/$class_name";
        $this->classes[$class_name] = new $class_name();
    }
}
// --- End Importer.php ---------------------


// --- Begin MyClass.php --------------------
class MyClass{
    public function get(){
        return "hello";
    }
}
// --- End MyClass.php ----------------------


// --- Where ever you call Importer ---------
$importer = new Importer();
$importer->import("MyClass");


echo $importer->MyClass->get();
这就是功能:

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
                {

                }   
            }
        }

    }
这样做的全部目的是在扩展时使包含的类在另一个类中可用

class Someclass extends Core
{
    public function somefunc()
    {
        $this->view->get(); // This does not work. 
    }
}
我怎样才能让它像这样工作?这是框架的一个非常重要的部分,因为这就是它的工作方式。我认为它在像CodeIgniter这样的流行框架中也能起到类似的作用

我试图使用
parent::view->get()
,但我想我没有完全理解它

我希望我能弄明白这一点,因为它让我在工作中陷入困境。
提前谢谢。

您要做的是使用“神奇的方法”,这是一种特殊的方法(\uu get()它获取外部无法访问的属性)。您将希望像这样使用它:

$core->import("someclass");
$core->import("view");
$core->view->get();
<?php
// --- Begin Importer.php --------------------
class Importer{
    protected $classes = array();

    public function __get($method_name){
        if(array_key_exists($method_name, $this->classes)){
            return $this->classes[$method_name];
        }
    }

    public function import($class_name){
        // Do this or use an auto loader
        require_once __DIR__ . "/../classes/$class_name";
        $this->classes[$class_name] = new $class_name();
    }
}
// --- End Importer.php ---------------------


// --- Begin MyClass.php --------------------
class MyClass{
    public function get(){
        return "hello";
    }
}
// --- End MyClass.php ----------------------


// --- Where ever you call Importer ---------
$importer = new Importer();
$importer->import("MyClass");


echo $importer->MyClass->get();

无论如何,“核心”是什么?是进口商吗?也许你应该叫它“导入器”,为什么不直接使用自动加载呢?更好的是,遵循PSR-0,使用标准的自动装弹机。@Waleed Khan;不,不,Core是一个核心类,它具有所有必要的功能。它应该包含在我框架中的每个php文件中。核心导入功能只是导入文件的一种更简单的方法。也许我以后可以重新设计类命名,但现在我必须集中精力使框架工作。@Rasteril OOP不是这样工作的。@Rasteril:自动加载是在需要时只包含所需的类,而不是一刻之前。这就是重点。