CakePHP Easter eggs

The documentation for CakePHP has grown in leaps and bounds since the creation of the cookbook. Today I wanted to look at a few methods and conventions that may not be crystal clear from reading the book or api.


Object is the parent class for almost all other classes in CakePHP. It provides some MVC agnostic functions that are often forgotten about. One such method that I’ve used a bit recently is Object::_set(). _set() is a protected method that allows the setting of multiple properties at once. It will only modify properties if they already existed in the class definition.

Show Plain Text
  1. function setProperties($properties) {
  2.     $this->_set($properties);
  3. }
  5. //
  6. $properties = array(
  7.     'mode' => 'paint',
  8.     'direction' => 'ASC',
  9.     'columns' => 4,
  10.     'rows' => 5
  11. );
  12. $myHelper->setProperties($properties);

The above would set the all the properties to $myHelper if they existed. It will ignore any properties that didn’t exist in the class declaration. This makes _set() a great way to quickly modify a lot of declared properties.


_stop() is basically a synonym for exit() however, it differs in that it makes testing easier. When testing your objects any exit() calls will terminate the script halting the testing process. If you use _stop() however, you can overload it in your Test Object and prevent the script from exiting, while maintaining the exit() in the live application.

Show Plain Text
  1. class TestPostsController extends PostsController {
  2.     function _stop() {
  3.         $this->exited = true;
  4.         return false;
  5.     }
  6. }

When you test this Controller any calls made to _stop() will set $exited which can be checked with an assertion. This makes methods with exit()‘s easy to test.


Object::dispatchMethod() is basically a drop in replacement for call_user_func_array(). It is slightly quicker than call_user_func_array so if you are using call_user_func_array you could benefit from using Object::dispatchMethod() instead.

Setting Component Config Variables.

Just before the feature freeze for 1.2 how Components could be declared was tweaked. The tweak made it so a config array could be passed into the Components initialize() method. Giving you the ability to declare Components more like Behaviors, either with or without a settings array.

Show Plain Text
  1. var $components = array(
  2.         'MyComponent' => array(
  3.             'cacheKey' => 'component_cache',
  4.             'userModel' => 'User',
  5.             'sessionKey' => 'MyComp'
  6.         ),
  7.         'Email', 'RequestHandler'
  8.     );

As seen above you can declare a list of parameters when declaring your Components. These parameters will be passed into your components intialize method, from there you can handle them however you wish. A nice and easy way to handle this is with the previously discussed Object::_set(), for example.

Show Plain Text
  1. function initialize($Controller, $settings) {
  2.     $this->Controller =& $Controller;
  3.     $this->_set($settings);
  4. }

This will set all the settings to the Component object. However, keep in mind that this syntax was added right at the end of 1.2 development and as such is not supported by any of the Core Components. That shouldn’t stop you from using it in your components though as it will save you a fair bit of space in your beforeFilter().

Passing parameters into Helpers from your Controllers

Much like Components, Helpers can also have parameters passed into them from the Controller declaration. These parameters are passed into the Helper’s __construct() method, where they can be handled.

Show Plain Text
  1. var $helpers = array(
  2.     'Ninja' => array(
  3.         'smokebombs' => true,
  4.         'shurikens' => 10,
  5.     ),
  6.     'Html', 'Form'
  7. );

Again this feature was added right at the end of 1.2 development, and therefore none of the core helpers implement it, which explains the lack of documentation related to it.

There aren’t any other easter eggs in CakePHP that I’m aware of. If you know of any please share them.


This is my first visit on your site, and I must say that I really like the content. Keep on blogging!

anonymous user on 9/5/08

Lucian: I’m glad you are enjoying it :)

mark story on 9/6/08

great article, mark. any new ones?

Jon on 12/31/08

Good job, keep walking…

MuertO LokO on 4/24/12

Comments are not open at this time.