重新声明php文件中的类,其中现在生成了一个函数

重新声明php文件中的类,其中现在生成了一个函数,php,Php,有一个文件myclass.php: <?php class Myclass { public static function Fun1() { } } 。。我得到一条消息:致命错误:无法在第12行的C:\xampp\htdocs\myproj\index.php中重新声明类MyClass。谢谢。“扩展”Myclass 另请参见:在同一命名空间中不能有两个同名的类声明。当使用引擎时,它如何知道这两个方法中的哪一个?您正在尝试向现有类添加一个方法?这与PHP非常不相似(更像

有一个文件myclass.php:

<?php
class Myclass {
    public static function Fun1() {

    }
}
。。我得到一条消息:致命错误:无法在第12行的C:\xampp\htdocs\myproj\index.php中重新声明类MyClass。谢谢。

“扩展”Myclass


另请参见:

在同一命名空间中不能有两个同名的类声明。当使用引擎时,它如何知道这两个方法中的哪一个?您正在尝试向现有类添加一个方法?这与PHP非常不相似(更像Ruby或JavaScript)。Runkit允许它:另请参见:@MichaelBerkowski这不像PHP,这是非常糟糕的OO。子类化
Myclass
在设计上会更好,不是吗?@wumm确实是个糟糕的OO,这就是为什么在链接问题中建议使用Decorator。
$class = "MyClass";
$function = "Fun1";
$fname = "myclass.php"
if (class_exists($class) && !method_exists($class, $functionName)) {
    $current = file_get_contents($fname);
    if ($current) {
        $strIndex = strlen($current);
        while ($strIndex-- && $current[$strIndex] != "}");//removing the last '}'
        $current = substr($current, 0, $strIndex);
        $current .= "\tpublic static function $functionName() {\n";//there inserting the new function
        $current .= "\n";
        $current .= "\t}\n";
        $current .= "}\n";
        $current .= "\n";
        file_put_contents($fname, $current);
        require $fname;//I load the file again, but how to redeclare the class?
    }
}
class MyMyClass extends MyClass
{
    public function SomeNewMethod() {
    }
}