Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5987133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:46:15+00:00 2026-05-22T22:46:15+00:00

I am working on an object to allow us to modify PHP files containing

  • 0

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 “deleteMethod()” to remove the PHP code for a specific method as follows: $this->deleteMethod("badMethod");. 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 ‘loadClass()‘ again to re-parse the updated object so it is ready for more editing. Below is an excerpt of the “deleteMethod()” 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 $this->deleteMethod("anotherBadMethod"); 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 loadClass(). 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 getFileName(); 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:46:16+00:00Added an answer on May 22, 2026 at 10:46 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a wrapper for a COM object that can only
I have mocks working where I test that methods on my mocked object are
I am working with an object which has sub objects within (see example below).
Imagine an object you are working with has a collection of other objects associated
I'm working on a project that has a rich object model with various sets
I have 2 different layout files that I want to use for modifying the
I am currently working on a filter in Grails that will allow me to
I am working on an object factory to keep track of a small collection
The object I’m working on is instantiated in JavaScript, but used in VBScript. In
I was recently working with a DateTime object, and wrote something like this: DateTime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.