有一个文件myclass.php:
<?php
class Myclass {
public static function Fun1() {
}
}
和我试着做一个自己的框架,在那里使用代码生成:
$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?
}
}
. .和我得到一个消息:致命错误:不能重新声明类MyClass在C:'xampp'htdocs'myproj'index.php在第12行。谢谢你。
"Extend" Myclass
class MyMyClass extends MyClass
{
public function SomeNewMethod() {
}
}
参见方法重载: