This article is 13 years old

The content here might be outdated as technology and best practices evolve quickly. While the concepts may still be valuable, please verify the information against current documentation and standards.

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

cakephp-tutorials tutorials

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

Frederik Jacques - Author

About the Author

👋 Hi, I'm Frederik Jacques, a passionate iOS developer and entrepreneur with over a decade of experience building innovative mobile applications.

I love creating apps that solve real problems and bring joy to users. From educational tools to creative meme makers, I'm always exploring new ways to blend technology with user experience. When I'm not coding, you'll find me sharing insights about iOS development, app monetization, and the indie developer journey.