[thelist] Classes, methods, return values and best practice

Andy Warwick mailing.lists at creed.co.uk
Wed Aug 21 17:58:01 CDT 2002


Appreciate some input from the list:

I'm fairly new to OOP (in PHP here, but I'm sure concepts apply equally to
other languages) but am making headway into the concept; however, I could do
with a few insights into best practice - and reasons why - for the
following:

So far, I've gleaned that all variables within the class are best being
'private', and only accessed through 'get' and 'set' methods, thus:

class Example {
    var $id= '' ;

    function GetId () {
        return $this->id ;
    }

    function SetId ( $value ) {
        $this->id = $value ;
    }
}

What I'm concerned with is what - if anything - each function should return.

So, in this example, is it better to have:

    function SetId ( $value ) {
        $this->id = $value ;
    }

or:

    function SetId ( $value ) {
        return $this->id = $value ;
    }

or:

    function SetId ( $value ) {
        if ( $this->id = $value ) return TRUE;
    }

or something else? Is there an agreed best practice here, and what are the
thoughts on why it should be so?

Is it also considered good practice to 'mistrust' passed in values and check
them, and if so what kind of checks are considered sufficient or ?

    function SetId ( $value ) {
        if ( $value = '' OR ! isset ( $value ) ) 'pseudo code chuck error'
        $this->id = $value ;
    }

Any insights/advice gratefully received.

Andy W





More information about the thelist mailing list