NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Methods and Properties

Dan Cech dcech at phpwerx.net
Wed Jul 28 00:16:58 EDT 2004


Phillip Powell wrote:
> Joe Crawford wrote:
> 
>> Phill,
>>
>> thanks that worked just as expected...
>>
>> Now anyone know of a way to make sure $part is actually one of the class
>> variables?

> 
> if (in_array($part, get_class_vars(get_class($this)))) { // DO STUFF }
> 
> Assuming you have instantiated a MyClass object prior to this line

Actually you would be better off with:

function setPart($part = NULL, $val = NULL, $append = FALSE) {
   if (isset($part) && isset($val) && @isset($this->{'_'.$part})) {
     if ($append) {
       $this->{'_'.$part} .= $val;
     } else {
       $this->{'_'.$part} = $val;
     }
     return TRUE;
   }
   return FALSE;
}

There is no need to go through the added overhead of calling 
get_class_vars and get_class, unless you want to disallow setting any 
vars not defined in the class definition.

Using the @isset(..) method you could define an addPart and delPart 
function if required, which you can not do with Phil's solution.

Dan




More information about the talk mailing list
Automatic Email Organization without missing anything!