Best Practises: Bug Fixing without Core Hacking

Posted by Felix Geisendörfer, on Feb 01, 2006 - in PHP & CakePHP » Core & Hacking

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Update: I fixed a minor bug in the code. I don't think it changes anything but I've forgot to pass some variable as a reference which it should be passed as. I also corrected the code snippet below.

One thing I've been confronted with since the first time I've used CakePHP is that there where bugs that I would need to fix in order to make my apps work. Now I could have just gone into the file and change the stuff around, but the problem was that all these fixes would be lost with the next update, no matter if the problem still existed or not.

So what I found was that most of the Problems can be traced back to Model, Controller, View (and Helpers). Here is where Cake's very good architecture comes in handy, since you can just use classes that extend your "Fix-Classes". But how does one exactly do that? Since I think it's something a lot of people will come across I've created a little Zip package called FlexiFix which contains a vendor folder with a fix_controller, fix_model and fix_view and ... (this was the most difficult thing), a folder called "helper" where you can put in files that will overwrite existing helpers like Html, but extend the original ones.

So if you want to try out my little package the download it here: (flexifix 0.2.zip) [3,57 kbyte].

If you want to do it yourself but are interested in the way I overwrite exisiting helper classes have a look at this snippet out of fix_view.php:

php
  1. function &_loadHelpers(&$loaded, $helpers)
  2. {    
  3.     $loadedHelpers = &parent::_loadHelpers($loaded, $helpers);     
  4.  
  5.     $fixHelperFolder = &new Folder(VENDORS.DS.'flexifix'.DS.'helpers');
  6.     $fixHelperFiles = $fixHelperFolder->find('^.*\.php$');
  7.  
  8.     $emptyLoaded = array();
  9.     foreach($fixHelperFiles as $num => $file);
  10.     {
  11.         list($helperName, $ext) = explode('.', $file);
  12.         $helperName = Inflector::camelize($helperName);
  13.         $fixHelperName = 'Fix'.$helperName;
  14.        
  15.         require_once VENDORS.DS.'flexifix'.DS.'helpers'.DS.$file;                                
  16.        
  17.         $fixHelper = &parent::_loadHelpers($emptyLoaded, array($fixHelperName));
  18.                        
  19.         $loadedHelpers[$helperName] = $fixHelper[$fixHelperName];
  20.         unset($fixHelper);
  21.     }                 
  22.     return $loadedHelpers;
  23. }

I hope this helps people to seperate their own bug fixes from the applications they write and therefore making updating an easier part of life.

--Felix