重新定义PHP类函数? [英] Redefining PHP class functions on the fly?

查看:83
本文介绍了重新定义PHP类函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何动态导入大量PHP类函数.例如...

I am trying to figure out how to import a large number of PHP class functions on the fly. For example...

class Entity
{
    public function __construct($type)
    {
    require_once $type."_functions.php"
    }

    // ...

}

$person = new Entity("human");
$person->sayhi();
$cow = new Entity("cow");
$cow->sayhi();

human_functions.php:

human_functions.php:

class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Hello world!";
        }
    }

cow_functions.php:

cow_functions.php:

class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Moo!";
        }
    }

我发现了一些可能性,例如 classkit_method_redefine() runkit_method_redefine() (它们是实验性",仍然无法修改当前正在运行的类).我目前使用的是PHP 5.3.3,因此无法使用特质(不确定那是否是我一直在寻找的东西).我已经成功重新定义了处理程序变量,如下所示:

I have found a few possibilities like classkit_method_redefine() and runkit_method_redefine() (which are "experimental", and they cannot modify the currently running class anyway). I am on PHP 5.3.3 right now, so I can't use Traits (Not sure if that is what I am looking for anyways). I have had success redefining the handler variable like this:

// Example 2:
class OtherEntity { /* Code Here */ }
class Entity
    {
    public function __construct($type)
        {
        global $foo;
        unset($foo);
        $foo = new OtherEntity();
        }
    }

$foo = new Entity();

但是,这感觉像是一种非常 hacky方法.更重要的是,如果我没有为类$foo每个实例命名,那么它将无法正常工作.我要做什么有任何解决方法?

But, this feels like a very hacky method. More importantly, if I don't name every instance of the class $foo, then it will not work. Are there any workarounds for what I am trying to do?

注意:我知道我可以扩展一个类,但是就我而言,当Entity类启动时,没有一种安全的方法可以提前知道要启动哪个子类.和.也许有一种我可以写的方法,例如:

Note: I am aware that I can extend a class, but in my case when the Entity class is initiated, there is no safe way to know in advance what subclass it would need to be initiated with. Perhaps there is a method I could write, such as:

public function changeClass
    {
    this->class = OtherEntity;
    }

感谢您的帮助!

推荐答案

以下是您可以尝试的可能解决方案的想法.让CowHuman类扩展Entity类.但是,Entity类将使用工厂实例化对象,基于值是否为安全的.让我们更详细地了解这一点:

Here's an idea of a possible solution you could try. Let the Cow and Human classes extend the Entity class. However, the Entity class would use a factory to instantiate the objects based on if the value was safe. Let's look at this in more detail:

/*
 * Class Entity should not be able to be instantiated.
 * It should contain a factory to instantiate the
 * appropriate entity and an abstract function declaring 
 * the method that each entity will need to implement.
 */
abstract class Entity {

    public static function factory($type) {
        return (is_subclass_of($type, "Entity")) ? new $type() : FALSE;
    }

    abstract public function sayHi();

}

/*
 * Human class extends Entity and implements the
 * abstract method from Entity.
 */
class Human extends Entity {

    public function sayHi() {
        echo "Hello World!";
    }

}

/*
 * Cow class extends Entity and implements the
 * abstract method from Entity.
 */
class Cow extends Entity {

    public function sayHi() {
        echo "Moo!";
    }

}

现在要使用此方法,请调用factory方法,如果一切正常,它将实例化将扩展Entity的适当类.

Now to use this method, call the factory method and if all works well, it'll instantiate the proper class which will extend Entity.

$person = Entity::factory("Human");
$person->sayHi();

$cow = Entity::factory("Cow");
$cow->sayHi();

使用is_subclass_of()可以确保您的安全,因为如果传入的值不是扩展Entity的类,则会为您返回FALSE的值.

Using, is_subclass_of() will keep you safe because if the passed in value is not a class that extends Entity, you'll be returned a value of FALSE.

如果您想查看上面的代码,请复制上面的php代码并在 phpfiddle.org <上进行测试/a>.

If you'd like to see the above code in action, copy the above php code and test it out on phpfiddle.org.

这篇关于重新定义PHP类函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆