Don Briggs Don Briggs - 10 months ago 42
PHP Question

PHP Object definitions cached? Trouble deleting methods with reflection

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()
if(!class_exists($this->class_name)) {
$this->error = "Class name: '$this->class_name' was not found.";}
else {
//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

I then use a function called "
" to remove the PHP code for a specific method as follows:
. This function then finds the start line and then end line of the method in question, deletes the line, saves the class PHP code back to disk, and runs '
' again to re-parse the updated object so it is ready for more editing. Below is an excerpt of the "
" function.

$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

The problem is that the object apears to be cached somewhere. When I run the
function again, it no longer returns the proper start/end lines for the next method to be deleted. After some checking, it became obvious that what is happening is that the when I try to get the start/end line for the next method to be deleted, PHP is still using the OLD class definition. It does NOT seem to "see" that some code has been deleted from the file, and the line numbers have changed. As you can see, I am instanciating both the object, and the reflection object every time we run
. And yes, I tried setting them to NULL before instanciating them.

I have also verified that PHP sees the class definition file properly. Meaning that even though the file was included, the reflection
does see that the class is defined where it should be.

Soooo.... Is PHP caching the definition of the class that I have included in memory? If so, how do I flush that cash? Is there some way to "undefine", that class, and reload it? Any help would be greatly appreciated.

Answer Source

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:

  • Work with the class definition on disk. This means you cannot use reflection but must use a tokenizer/parser instead.
  • Tamper with the file so that the class is reloaded in another namespace. This will greatly pollute your global namespace with a lot of other use-once namespaces, since once they're defined, they cannot be unloaded.
  • Execute the script that loads/modifies the class in a separate process. When the process starts, it will load the class definition and will forget all about it when it terminates, producing the "refresh" effect you're looking for. This is obviously your best bet.