Declaration of beforeSave() should be compatible with Model::beforeSave( $options = array() )

I’ve just upgraded my servers to PHP 5.4 and got this error when loading a project.

Declaration of MyModelName::beforeSave() should be compatible with Model::beforeSave( $options = array() )

There seems to be a problem when using PHP 5.4 and CakePHP 2.1 which results in this error.  Since PHP 5.4 E_ALL and E_STRICT are now combined and with a default CakePHP installation error reporting is set to E_ALL & ~E_DEPRECATED!

Luckily it is really easy to fix!

Fix 1:

Like most times in programming … read the friggin error message 🙂
As you are told … add the argument which is required in the function callback its signature:

[crayon lang=”php” title=”Fix 1: Add argument”]
// Old code
public function beforeSave( ) {
// Awesome code

return true;
}

// New code
public function beforeSave( $options = array()  ) {
// Awesome code

return true;
}

[/crayon]

Fix 2:

Configure the error handler configuration key in your core.php file to

[crayon lang=”php” title=”Fix 1: Adjust Error key in core.php”]
// Old version
Configure::write(‘Error’, array(
‘handler’ => ‘ErrorHandler::handleError’,
‘level’ => E_ALL & ~E_DEPRECATED,
‘trace’ => true
));

// New version
Configure::write(‘Error’, array(
‘handler’ => ‘ErrorHandler::handleError’,
‘level’ => E_ALL & ~E_NOTICE & ~E_STRICT,
‘trace’ => true
));
[/crayon]