NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static variables, self:: and abstract classes

Andrew Kamm akamm at demicooper.com
Thu Jun 8 07:45:28 EDT 2006


>>> just one question had crossed my mind right now...
>>> 
>>> why did u had declared the variable as static?
>> 
>> The idea was to have an abstract parent class for customer preferences since
>> most preference classes would basically share the same code.  The static
>> vars (or constants) that were to be stored in the individual classes were
>> properties that differentiate how the child classes would interact with the
>> database (among other things).
> 
> Static is the wrong approach.  So scratch what I said in my prior email, I
> was just trying to prove it could be done.
> 
> Declare the methods in the parent and the properties in the children.
> Then instantiate your class into an object.  You get your desired outcome
> without a fake constructor plus you don't have to maintain properties in
> the parent that are just going to get overridden.
> 
> class base {
>     public function getTablename() {
>         echo $this->_tablename . "<br/>\n";
>     }
> }
> 
> class joes extends base {
>     protected $_tablename = 'joestable';
> }
> 
> $obj = new joes;
> $obj->getTablename();
> 
> --Dan


Actually, you were pretty right on with the other suggestion in terms of
this particular implementation (the other method being to use a 'setVars()'
method to set the static vars in a class that is being referenced
statically).

Either way would work, but I want to limit instantiating an object unless
it's really going to be utilized as an object (contrary to the email I sent
a couple days ago about instantiating objects with the string 'static'
rather than the numeric ID they should use).

Basically, I probably should have structured those classes differently, but
I didn't anticipate the whole inherited 'self::' calls referencing the
parent rather than, well... 'self::'. I always thought that's what parent::
was for. I still scratch my head at the logic of that -- I'd love to see an
example of it in a beneficial situation so I could look at it as a
functionality rather then a limitation of PHP.

Thanks for all the help, it's appreciated.


-- 
Andrew Kamm





More information about the talk mailing list