[nycphp-talk] Static variables, self:: and abstract classes
akamm at demicooper.com
Tue Jun 6 13:33:49 EDT 2006
Just an update my hack to get around this was to allow instantiation of
empty objects that are used for calling what would be static methods.
The classes would normally accept a numeric $id var in their constructor
(the constructor being defined in the parent class) --- I¹ve adapted them to
accept the string static¹ as well. If static¹ is the argument for the
constructor, they object is created without any of the normal properties it
would have as an individual object¹, but it¹s able to call methods defined
in the parent class and have them be relevant to the child class (i.e., in
the parent class there are no more references to ³self::XXXX², just
references to ³this->xxxx².
The methods within the parent class are no longer static.
It¹s a hack, but it beats maintaining several files with the exact same
From: Wolfy <the.wolfy at gmail.com>
Date: Tue, 6 Jun 2006 13:42:17 -0300
To: NYPHP Talk <talk at lists.nyphp.org>, <akamm at demicooper.com>
Subject: Re: [nycphp-talk] Static variables, self:: and abstract classes
well. i´ve left php exactly for this "problem", the solution well, it´s a
on java, when u extends a class the child class inherits all methods and
attributes, and you still have the option to overwrite or override the
parent method. but, i dont know why php does not let you override a method
(and, i´m not sure, but does not let you overwrite a method too).
so, what can u do?
you can change the static method to a non-static one and try to overwrite
the method to get the child attribute in the method.
or you can try testing the instance of the class. just like this
if (obj instance of parent1) call method from parent1
elseif (obj instance of parent2) call method from parent2 and so on
if this does not help you. let me know... and then i can make further test
to get a solution
On 6/6/06, Andrew Kamm <akamm at demicooper.com> wrote:
>> > Actually, I'm getting:
>> > Fatal error: Cannot redeclare property static protected
>> > ParentClass::$myVar in class ChildClass
>> > for your example.
> Yeah, I may have missed how I originally coded it -- I've been trying
> several different methods so I probably got confused.
> Ultimately, it comes down to any use of a "self::XXXX" reference in a parent
> class method will cause a child class to use the parent's value for
> Most recently, I tried assigning constants in the parent class to the value
> of __CLASS__. Sure enough, when those constants were used in a static method
> and called by a child class, the value of the constants were the name of the
> parent class, not the class actually calling the method(!).
> I'm thinking I may have to develop a separate manager class to handle the
> functions. Kind of a drag, but I really don't want to cut and paste the
> exact same methods into 10 child classes.
> I'm not seeing the upside to this implementation of self::. How does Java
> handle this?
> Andrew Kamm
> New York PHP Community Talk Mailing List
> New York PHP Conference and Expo 2006
> Show Your Participation in New York PHP
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the talk