I have updated my machine from PHP 5.3 to PHP 5.4 and my CakePHP (version 2.1.1) app keeps getting these errors:
Fatal Error: Class ‘String’ not found in …/Behavior.php on line …
Fatal Error: Class ‘Debugger’ not found in …/Component.php on line …
Etc.
The errors are really not useful, since the LINE and FILE of the error always state the end bracket of the class declaration.
After a lot of searching, I managed to solve SOME of the errors by adding proper public/private function declarations and strong parameter typing, A.K.A.:
function beforeSave($Model) {}
//becomes
public function beforeSave(Model $Model){}
After transferring to PHP5.4, you have to cleanup a lot of “bad practices” in your code.
Strong-typed parameter definition in extended methods
This means that you have to write the class of the objects which the method receives just before the variable name. This is only needed for methods which replace parent’s class methods, not for all methods. If unsure, just check the method declaration in the parent class in the core files or API.
Remove “Call-time pass by reference”
I’ve seen a lot of people passing arround Controller and Model objects in functions BY REFERENCE:
This throws an error and is wrong.
Just remove the ampersand before the variable. You will not break any functionality, because Objects are already passed by reference in PHP, and as I get it, this was removed in PHP 5.4.
Declare all method parameters in extending methods
If you overwrite a parent-class’ method, you should declare all function parameters in the function definition. If there is a parameter missing, you will get an error.
Example:
Add App::load() to load dependencies
Always good to check if your class is really available, so double-check if you have all the classes you depend loaded with App::load() at the beginning of the file.