Are PHP object definitions cached? Ways to remove errors with reflection

I am working on an object that allows us to modify PHP files containing PHP objects. (In particular, these are Doctrine entity files that we must modify.)

In any case, without the boring details, what happens here is happening. First, I find the location of the class file and TURN ON it. Then I instantiate the class and the class reflector using the code below. As you can see, when I instantiate an object and a reflector, I also call a method to load the text of the class from disk into a string and another method to split this string into an array of rows.

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
   }
}

"deleteMethod()", PHP- : $this->deleteMethod("badMethod");. , , , PHP 'loadClass()', , . "deleteMethod()".

$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->deleteMethod("anotherBadMethod");, , . , , / , PHP OLD . , , - , . , , , loadClass(). , NULL, .

, PHP . , , getFileName(); , , .

Soooo.... PHP , ? , ? "undefine", ? .

+3
4

, . PHP . "" - script . , , , " ". , script , , ( ).

API Reflection , .

, :

  • . , , /.
  • , .. , , , .
  • script, / . , , , ", . , , .
+4

loadClass() , , PHP. , class_exists() PHP.

, "" , script. script .

, :

  • script , . script . , script, .
  • runkit ( PECL), .
0

! . , , , , . " ", , . .

private $del_text_buffer   = array();  //Array holding text to delete

, , . , , . , .

, loadClass(). , . class_text class_array. , , . . .

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->class_text      = file_get_contents($this->source_file);
        $this->class_array     = explode("\n", $this->class_text);
    }
}
$array_extract, .
private function array_extract($haystack, $start_index, $end_index){
    $result = false;
    if(is_array($haystack)) {
        $extract = array();
        $n = 0;
        for($i=$start_index; $i<=$end_index; $i++) {
            $extract[$n] = $haystack[$i];
            $n++;
        }
        $extract = implode("\n", $extract);
        $result = $extract;
    }
    return $result;
}
, , , , , array_extract()., . , .
public function deleteMethod($meth_name = "") {
    $result = false;
    $oMethod = $this->oRClass->getMethod($meth_name);
    //Find where method is located, and hack it out
    $start_index  = $oMethod->getStartLine() -1;
    $end_index    = $oMethod->getEndLine() -1;
    $del_string = $this->array_extract($this->class_array, $start_index, $end_index);
    array_push($this->del_text_buffer, $del_string);
    $this->num_changes++;
    //--- Delete the comment attached to the text, if any
    $comment_txt = $oMethod->getDocComment();
    if($comment_txt != "") {
        array_push($this->del_text_buffer, $comment_txt);
        $this->num_changes++;
    }
}
, deleteMethod() $class_text $class_array. , , , .

, , saveObject(). . , .

public function saveObject() {
    if($this->num_changes) {
        $num_recs = count($this->del_text_buffer);
        for($i=0; $iclass_text = str_replace($this->del_text_buffer[$i], "", $this->class_text, $changes_made);
        }
        $result = file_put_contents($this->source_file, $this->class_text);
        $this->num_changes = 0;
    }
}
0

I had this problem today, and I came across it for a while, until I came across this topic, and therefore found it impossible. However, there is a good way: just edit the functions / methods in your file in the reverse order from which they were defined. Thus, the lines of the beginning and the end are always true. You just need to document this for future people who are learning your code.

0
source

All Articles