There is a file myclass.php:
<?php
class Myclass {
public static function Fun1() {
}
}
and i try to make an own framework where to use a code generation:
$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() {
";//there inserting the new function
$current .= "
";
$current .= "\t}
";
$current .= "}
";
$current .= "
";
file_put_contents($fname, $current);
require $fname;//I load the file again, but how to redeclare the class?
}
}
.. and i get a message: Fatal error: Cannot redeclare class MyClass in C:\xampp\htdocs\myproj\index.php on line 12. Thank you.
"Extend" Myclass
class MyMyClass extends MyClass
{
public function SomeNewMethod() {
}
}
see also Method Overloading: