简体   繁体   English

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

[英]How to use an instance of a class assigned to a property of another class in the other class in php

I am making a framework in PHP. 我正在用PHP创建一个框架。 I have an import function in library/core.php. 我在library / core.php中有一个import函数。

I can use the function like this: 我可以使用这样的功能:

$core->import("someclass");

This is the function: 这是功能:

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
                {

                }   
            }
        }

    }

So in an other class, I can use it like this: 所以在另一个类中,我可以像这样使用它:

$core->import("view");
$core->view->get();

The whole point of this, was to make the included class available in another class, when it is extended. 这一点的全部意义在于,当扩展时,使包含的类在另一个类中可用。

class Someclass extends Core
{
    public function somefunc()
    {
        $this->view->get(); // This does not work. 
    }
}

How could I make it work like this? 我怎么能让它像这样工作? This is a very important part of the framework, because this is how it works. 这是框架的一个非常重要的部分,因为它是如何工作的。 I think it works similar in popular frameworks like CodeIgniter too. 我认为它在CodeIgniter等流行框架中的工作方式也类似。

I was trying to use parent::view->get() , but I guess I don't fully understand it. 我试图使用parent::view->get() ,但我想我并不完全理解它。

I hope I can figure this out, because it is holding me down in my work. 我希望我能弄明白这一点,因为它让我失去了工作。 Thank you in advance. 先感谢您。

What you want to do is use "Magic Methods", this particular one (__get() this gets properties not accessible from outside). 你想要做的是使用“魔术方法”,这个特殊的方法(__ get()这会得到无法从外部访问的属性)。 You will want to use it like this: 你会想要像这样使用它:

<?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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM