NYCPHP Meetup

NYPHP.org

[nycphp-talk] object getter/setter methods

Flavio daCosta nyphp at n0p.net
Fri Mar 11 12:45:33 EST 2005


I too am curious the consensus of this question.  I have thought of
implementing something similar to the following, but havent yet.

I know this can be simplified in php5, but this gives similar
functionality in php4...

Flavio


-- untested code --
function get( $var )
{
    if( method_exists( $this, 'get' . $var ) )
    {
        return call_user_function( array( $this, 'get' . $var) );
    }

    $public_properties = array( 'exposed_var1', 'exposed_var2' );
    if( in_array( $var, $public_properties ) )
    {
        return $this->{$var};
    }
    return null;
}

function set( $var, $value )
{
    if( method_exists( $this, 'set' . $var ) )
    {
        return call_user_function( array( $this, 'set' . $var) );
    }

    $public_properties = array( 'exposed_var1', 'exposed_var2' );

    if( in_array( $var, $public_properties && isset( $this->{$var}) )
    {
        $this->{$var} = $value;
    }
}
-- end untested code ---




More information about the talk mailing list