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:

// Old code
public function beforeSave( ) {
// Awesome code

return true;
}

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

return true;
}
PHP

Fix 2:

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

// 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
));
PHP
Share this post

13 Responses

  1. Note for Fix 1:
    Don’t forget the $-sign in front of the options variable:
    function beforeSave( $options = array() )

    But thanks for the hint!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts