动态重新定义PHP类函数?

动态重新定义PHP类函数?,php,oop,Php,Oop,我试图弄清楚如何动态导入大量PHP类函数。例如 class Entity { public function __construct($type) { require_once $type."_functions.php" } // ... } $person = new Entity("human"); $person->sayhi(); $cow = new Entity("cow"); $cow->sayhi(); human_f

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

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:

class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Hello world!";
        }
    }
class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Moo!";
        }
    }
cow_functions.php:

class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Hello world!";
        }
    }
class Entity redefines Entity
    {
    public function sayhi()
        {
        echo "Moo!";
        }
    }
我发现了一些可能性,比如and(它们是“实验性的”,它们无论如何都不能修改当前运行的类)。我现在使用的是PHP5.3.3,所以我不能使用(不确定这是否是我想要的)。我成功地重新定义了如下处理程序变量:

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

$foo = new Entity();
但是,这感觉像是一个非常粗糙的方法。更重要的是,如果我没有命名类
$foo
的每个实例,那么它将无法工作。我想做的事有什么解决办法吗

注意:我知道我可以扩展一个类,但是在我的例子中,当实体类被启动时,没有安全的方法可以预先知道它需要用什么子类启动。也许我可以写一个方法,比如:

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

谢谢你的帮助

您可以做的一件事是创建
Human
Cow
作为
实体的子类。执行
新建实体(“人员”)
时,可以在
实体
实例中存储新创建的
人员
对象

然后您可以使用
\u call
将方法调用重定向到“子元素”


唯一的缺点是
$person
$cow
都是
实体
对象。

下面是一个您可以尝试的可能解决方案。让
Cow
Human
类扩展
实体
类。但是,
实体
类将根据值是否安全来实例化对象。让我们更详细地了解一下:

/*
 * 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!";
    }

}
现在要使用这个方法,调用工厂方法,如果一切正常,它将实例化适当的类,该类将扩展
实体

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

$cow = Entity::factory("Cow");
$cow->sayHi();
使用,
is\u subclass\u of()
将确保您的安全,因为如果传入的值不是扩展
实体的类
,您将返回一个
FALSE


如果您希望看到上述代码的实际应用,请复制上述php代码并进行测试。

对于您的实际问题,肯定还有另一种解决方案。您的第一个示例建议了,但我不确定您真正想要实现什么。是的,您做出了一些需要重新思考的设计选择。是
重新定义实际的php函数。我相信你在寻找
扩展
@CodeGodie不,不是,比如我能理解的目的。公认的答案非常适合这种情况。:)进一步阅读:那将是一种形式的。你就是那个人。非常感谢。这正是我想要的。为什么要抽象地定义
sayHi()功能?即使在原始的
实体
类..@DarthCaniac中没有定义它,它似乎也能工作,这是正确的。绝对会的。
abstract
关键字的好处是,任何使用
抽象函数扩展基类的类都必须在自己的类中实现该方法。换句话说,
Human
Cow
都必须实现
sayHi()
,否则php解释器将抛出错误。这保证了任何扩展
实体的类
都将包含一个有效的
sayHi()
方法,该方法可以为该类执行特定的操作。Lol非常抱歉。这篇评论是为作品而写的。手机软件太差劲了。哈哈,不用担心。我也这么想。我只是想看看你有没有推荐的编辑。事实上,你的建议是我无论如何都会做的。抽象类就是为此而创建的。我会投票的,谢谢:)