I am working on an object to allow us to modify PHP files containing PHP objects. (Specifically, they are Doctrine entity files that we have to modify.)
Anyway, without the boring details here is what is happening. I am first finding the location of the class file, and INCLUDEing it. I then create an instance of the class, and a class reflector, using the the code below. As you can see, when I instantiate the object and the reflector, I also call a method to load the text of the class from disk into a string, and another method to break that string into an array by lines.
public function loadClass()
$this->error = "Class name: '$this->class_name' was not found.";}
//Class is found. Load it and populate properties
$this->oClass = new $this->class_name;
$this->oRClass = new \ReflectionClass($this->oClass);
$this->source_file = $this->oRClass->getFileName();
$this->error = "";
$this->class_namespace = $this->oRClass->getNamespaceName();
$this->getClassText(); //Load class code from source file
$this->getClassArray(); //Load class text into array
$oMethod = $this->oRClass->getMethod($meth_name); //Get a refection method object
$oMethod->setAccessible(true); //In case method is private
$start_line = $oMethod->getStartLine() -1; //Line method starts at
$length = $oMethod->getEndLine() - $start_line + 1 //Number of lines in method
array_splice($this->class_array, $start_line, $length); //Hack lines out of array
$this->class_text = implode("\n", $this->class_array); //Convert array back to a string
$this->saveClassText(); //Save updated code back to disk.
$this->loadClass(); //Reload and reparse for consistancy
This is not possible the way you put it. PHP can only load a class definition once. After it's loaded and in memory, the only way to "refresh" it is to terminate the script and re-execute it. If you try re-including the file, you'll obviously get a "class already defined error". No matter how long your script runs, once the class definition is in memory, there's nothing you can change in it (unless you use a third-party extension).
The Reflection API works on the class definition in memory, not the one on the disk.
I think you have three options from here: