NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Methods and Properties

Joe Crawford jcrawford at codebowl.com
Wed Jul 28 21:33:36 EDT 2004


Dan,

i do not want the class setting any undefined properties.

Joe Crawford Jr.


On Wed, 2004-07-28 at 00:16, Dan Cech wrote:
> 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
> 
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
> 
> 




More information about the talk mailing list