RSS
 

Posts Tagged ‘Access’

Useful PHP coding tips

03 Feb

I hope the following tips help some of the newer php coders and prevent errors I either had to face or did myself.

Don’t use private attributes without a good reason

There are some libs or classes that use private attributes for no good reason. It makes it impossible to extend and modify this class.

Example:

class Doc {
    private $title = "";
    private $language = "en";
    ...
}

Now what if we want to add another method by using “extends”?

class MyDoc extends Doc {
    ...
}

We cannot access any of the attributes and have to redeclare them and stuff. So: protected is more than enough.

Additionally, many still add the _ prefix for easier identification (although I am not sure what is the better practice here^^):

class Doc {
    protected $_title = "";
    protected $_language = "en";
    ...
}

Now everything’s nice and extendable!

Same goes for 99% of all class functions. The only case where private attributes make sense is when a private (for whatever reason) method is the only method accessing an attribute. Then we can make this one private, as well.

Using too much private attributes and methods even is kind of arrogant, if you think of it. You are making assumptions of what parts are so perfect that they will never have to be overridden. Impossible for all possible scenarios. At least for any class that is even remotely likely to be extended (which usually all are).

Don’t count on arrays to start with int 0

Well, at least in some places it can easily be avoided to expect the array to have integer array keys starting with 0 (zero) or a specific key name for that matter. One good example (it even shortens the code) is the custom model validation rules:

/**
 * usage: rule => array('validateIcq')
 * @return boolean $success
 * note: $data is in format: array('fieldname'=>'value')
 */
function validateIcq($data) {
    $value = array_shift($data);
    //validate
}

It doesn’t matter if the array key is 0, 1, 2 or “whatever”. We are only interested in the array value. So why bother using $value = $data[0] or $value = $data['fieldname']? In most cases where the array is pre-processed and could have some array values removed this can end in unexpected behavior if we want to get the first record. In my example from above the keyword would be the current validation field name. but it still could change – with this approach it is always working without error.

Note: array_shift() removes the current value. There are other methods which won’t do that. In any case, don’t simply put this $data[0] stuff in if there might be the slightest change it could also be 1 or 2 already. Of course there are some appropriate cases for key access – but in most cases there are not, causing unnecessary errors.

Using current() and reset() you can easily go through any array that might have some “gaps”. Also note that foreach() works perfectly here, too. Hard-coded keys only if the content of the array is reliably structured in all cases. Same with for($i = 0; $i < $x; $++) loops.

Remember to call parent methods

class MyFoo extends Foo {
    ...
    function __construct($paramX, $paramY = null) {
        parent::__construct($paramY, $paramY); # with all contructor params!
     //your overriding content
    }
}

This is especially important for overriding model constructors in CakePHP!

Another example are callbacks like beforeFilter(), beforeRender() etc:

function beforeRender() {
    parent::beforeRender();
    //your overriding content
}

I usually call the parent ones first, then move on to the overriding part. This can depend on the current case sometimes, though.

More

// to be continued – maybe // you got sth i missed? drop my a line

 
2 Comments

Posted in PHP