SanSaurus
SanSaurus

Reputation: 163

How do I avoid magic strings (PHP MVC Frameworks)

I want to use a MVC-framework together with PHP to make a nice separation between code and presentation. I've currently started to look at CakePHP. It looks nice, but whats about all those magic strings?

Take a look at the code below (taken from the CakePHP cookbook):

class User extends AppModel {
    var $name = 'User';
    var $hasOne = 'Profile';
    var $hasMany = array(
        'Recipe' => array(
            'className'  => 'Recipe',
            'conditions' => array('Recipe.approved' => '1'),
            'order'      => 'Recipe.created DESC'
        )
    );
}

It bothers me with those magic strings. Strings in code ought only to be text to be outputted!

One spelling mistake in 'Recipe.created DESC" and it won't work as expected. There's no intellisense/code completion to help here!

Also, what if I want to remane 'Recipe' to something else? I have to search manually through all the code and find out if it's regular text or one of the magic strings.

Are other PHP MVC-frameworks better? (read: less or no magic strings)

Any links on how to avoid magic strings (at least as much as possible)...?

Upvotes: 2

Views: 939

Answers (3)

Wayne Baylor
Wayne Baylor

Reputation: 169

I see what you did there

Also, what if I want to remane 'Recipe'...

No matter which ORM library you choose there will always be magic strings. There just isn't enough information to automatically map your class to a database table (except in the simplest cases). The magic strings are really meta-data that the ORM needs to create the mapping.

You can try to avoid magic strings by defining constants or creating public static fields, but ultimately you will have only consolidated them to, at best, a single file.

When working with an ORM you will have to provide meta-data no matter what. With that in mind, you should choose the ORM that feels most comfortable to you.

Upvotes: 0

JohnP
JohnP

Reputation: 50019

Cake follows the 'Convention over Configuration' methodology. It's a framework that can be used to rapidly prototype and deploy (mostly) CRUD based applications. It's got a lot of 'magic' happening behind the scenes which don't always make it a good choice if you don't know what's going on under the hood.

As to your specific example, it isn't too bad. Recipe actually refers to a Recipe model that the User model is related to. Cake has an inbuilt ORM that uses some model variables to set your model relations properly. This would be the case in any ORM framework, I'd say. Unless you changed the class name, you wouldn't need to change all the references, this would be true in any PHP code.

As to other framework recommendations. I'd suggest that you work with your own PHP MVC stack before you begin to work with frameworks. If you have a fair understanding of PHP, you could look at the CodeIgniter or Kohona framework. These are less rigid than Cake. You could also have a look at the Zend Framework. There are many other frameworks out there that have different features (more or less with differing variations of control offered), all you need to do is look around.

Upvotes: 2

Related Questions