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::_set().
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.
- function setProperties($properties) {
- $this->_set($properties);
- }
- //
- 'mode' => 'paint',
- 'direction' => 'ASC',
- 'columns' => 4,
- 'rows' => 5
- );
- $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.
Object::_stop()
_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.
- class TestPostsController extends PostsController {
- function _stop() {
- $this->exited = true;
- return false;
- }
- }
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()
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.
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.
- function initialize($Controller, $settings) {
- $this->Controller =& $Controller;
- $this->_set($settings);
- }
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.
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