Yii 1.1: Patching PHP code (used as a library) without changing the original code!


The article Fixing extensions without modifying their code reminded me of another patching technique that I used. My conviction and a hard look on the web had led me to an article Redefine PHP class methods or class giving me a head start to do what I wanted: patching third party code without touching the original to allow easy upgrades.

The trick used here to patch PHP code is to use PHP's "eval" function on PHP code patched inline.

Here is an example of how to use the Patch class that is provided further below. The first piece of code redefines a function in a class. The second piece of code redefines an entire class. The third piece of code patches some code using a 'str_replace'

$objPatch = new Patch('path_to_class_file.php');
   protected function foo(\$arg1, \$arg2)
       return \$arg1+\$arg2;
$objPatch = new Patch(dirname(__FILE__).DIRECTORY_SEPARATOR.'pathtoclass.php');
$objPatch = new Patch("anotherclass.php");
// Next line disables the include of the patched file */
$objPatch->replaceCode('include_once ("PHP','$_dummy=("');
// Next line patches the file_exists condition (modify the path if the path does not exist)

The Patch class is next (I believe I patched that class a bit myself ;-) ).

 * Based on: http://stackoverflow.com/questions/137006/php-redefine-class-methods-or-class
 * Usage:
 * $objPatch = new Patch('path_to_class_file.php');
 * $objPatch->redefineFunction("
 *   protected function foo(\$arg1, \$arg2)
 *   {
 *       return \$arg1+\$arg2;
 *   }");
 *   or use 'redefineClass()"
 * Then eval the new code:
 * eval($objPatch->getCode());
class Patch {
    private $_code;
    public function __construct($include_file = null) {
        if ( $include_file ) {
    public function setCode($code) {
        $this->_code = $code;
    public function includeCode($path) {
        $fp = fopen($path,'r');
        $contents = fread($fp, filesize($path));
        $contents = str_replace('<?php','',$contents);
        $contents = str_replace('?>','',$contents);
    function redefineFunction($new_function) {
        preg_match('/function (.+)\(/', $new_function, $aryMatches);
        $func_name = trim($aryMatches[1]);
        if ( preg_match('/((private|protected|public) function '.$func_name.'[\w\W\n]+?)(private|protected|public)/s', $this->_code, $aryMatches) ) {
            $search_code = $aryMatches[1];
            $new_code = str_replace($search_code, $new_function."\n\n", $this->_code);
            return true;
        } else {
            return false;
    function replaceCode($oldcode,$newcode) {
    function redefineClass($oldclass,$newclass) {
        if(preg_match("/(class $oldclass)/", $this->_code, $matches)) {
            $new_code = str_replace($matches[1],"class $newclass",$this->_code);
            return true;
        return false;
    function getCode() {
        return $this->_code;

Be the first person to leave a comment

Please to leave your comment.

Write new article
  • Written by: le_top
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +5
  • Viewed: 6,173 times
  • Created on: Feb 28, 2013
  • Last updated: never
  • Tags: patch, patching