NYCPHP Meetup

NYPHP.org

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

Andrew Kamm 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
code. 

-- 
Andrew Kamm



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


Hi Andrew,

well. i´ve left php exactly for this "problem", the solution well, it´s a
different history.

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 

[]´s
gibaWolfy
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
> "self::XXXX."
> 
> 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
> http://lists.nyphp.org/mailman/listinfo/talk
> New York PHP Conference and Expo 2006
> http://www.nyphpcon.com
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20060606/9c85a417/attachment.html>


More information about the talk mailing list