Learning to Work With (and Not Against) Your PHP Framework

by | Aug 23, 2016

Too often have I come upon a code base, in a framework that I know, just to be lost in a mess of custom-written code. If you leverage the framework of choice, future developers of the code base will appreciate it, as they only have to learn the framework and not cryptic custom code. Every line of code written outside of the pattern of the framework is another line of code that has to be understood. We’ll take a look at common mistakes and their resolutions.

Reinventing the Wheel

For most of the matured PHP Frameworks (Laravel, Yii, etc.), you get a fully functional MVC (Model View Controller) style framework out of the box. This means that basic actions like getting request data (GET/POST) can be handled through your framework. There is rarely a need to directly use $_GET or $_POST, as frameworks will have their own methods that provide additional levels of security and cleansing, rather than relying directly on these variables.

Helpers are for Helping

Laravel and Yii 2 both have extensive documentation regarding helpers for actions that revolve around URL generation, string modification, and array tweaks. If you are trying to find text at the beginning of a string, and hacking around using substr function, chances are you can leverage either starts_with (Laravel) or startsWith (Yii2) instead of having the alternative; complicated substr and strpos logic within your code base.

At some point in a project, you will have to deal with arrays and modify them. Whether this is recursive sorting, re-indexing, or even plucking certain values, the logic proof code for these actions can quickly balloon into large blocks of code. Frameworks have community tested functions to do most of these actions, you just have to find them and use them.

A common task for me is to re-index an array based on a value inside each child record. This ends up looking something like this:

$return = [];
foreach ($data as $item) {
    $return[$item['key']] = $item;
}
return $return;

These few lines can be replaced with Yii’s Array Helper, like:

return ArrayHelper::index($data, 'key');

Which helps explain the purpose of what I’m doing and removes 4 lines of code.

ORM & Database

I recently reviewed a block of code that wrote a simple query for finding the profile of a user. While the logic was right, this function duplicated existing functionality and added about twenty lines to the project. This specific example could be resolved by leveraging the relations of the User model, like $user->profile. This clean-looking call relied on the magic functions of the model class and the relation/foreign-key setup ahead of time. Both Laravel and Yii2 help you accomplish this, so you can simply call a relation (one or many) via $model->relation, instead of reverting to writing queries to accomplish such a task.

Conclusion

As you work in a framework more, you will naturally pick up its helping functions and shortcuts to basic actions. For those new to a project or framework, it doesn’t hurt to spend a few minutes searching the official guide of that framework. If you can find framework functions that duplicate the functionality you require, this will help to keep your project small and easier to read for future developers.

 

 

Recent Posts