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;
}
PHPFix 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
13 Responses
Note for Fix 1:
Don’t forget the $-sign in front of the options variable:
function beforeSave( $options = array() )
But thanks for the hint!
Fixed! Thanks.
Thanks for this fix!
Hello,
Thanks for the fix, it’s very helpfull !
I was having the same issue with Laravel. You provided the solution to it. Thank you very much. I have bookmarked your blog.
Ravi.
thanks a lot
Nice 🙂
Excellent, this work like a charm for me !
Thank you.
This caught me out on an upgrade to php55u. Thanks
It works. Thanks.
Great Works. Thanx 🙂
Thank you very much, your information was exceptionally useful!
greetings from Ecuador!