Page 1 of 1

Overriding static members or Accessing child members

Posted: Tue Aug 21, 2007 7:38 am
by anjanesh
Im looking for a solution where an inherited class has a static member, but should be accessible in a method in the parent class.

Code: Select all

<?php
class X
 {
        protected static $m;
 
        public function println()
         {
                echo self::$m;
                echo "\n";
         }
 
 }
 
class B extends X
 {
        public function __construct()
         {
                parent::$m = 5;
         }
 }
 
class C extends X
 {
        public function __construct()
         {
                parent::$m = 6;
         }
 
 }
 
$b = new B();
$c = new C();
 
$b->println();
$c->println();
?>
This obviously produces 6 and 6 but Im looking for a solution that outputs 5 and 6.
Is there any way for B's static variable to hold 5 and C's static to hold 6 ? Overriding static member in inherited class... ?
I need a static variable for all decendants of X, but require println() function to be in X.

Or, all I need is the opposite of parent - child. something that I can do as if a scope resolution operator for child exited (child::).

Code: Select all

class X
 {
        public function set($value)
         {
                child::$z = $value;
         }
 
        public function println()
         {
                echo child::$z;
                echo "\n";
         }
 }
 
class B extends X
 {
        public static $z;
 
        public function __construct()
         {
                parent::set(5);
         }
 }
 
class C extends X
 {
        public static $z;
 
        public function __construct()
         {
                parent::set(6);
         }
 }
Is there a way to make either possible ? If not php, does any language support accessing child members from parent ?

Thanks

Posted: Tue Aug 21, 2007 8:02 am
by superdezign
Have you tried simply overwriting it? You aren't making much sense by not wanting the children to use the same value of the parent, but somehow have the parent use the same value as the children. You give no specification as to which child the parent would even be referring to.

Posted: Sun Aug 26, 2007 9:01 am
by anjanesh
For php snippet #1,
Python seems allow such a thing (some different kind of static method) using something known as class methods which are a bit different from static methods !?%^~#@*

Code: Select all

class X:
      def set(cls, value):
          cls.m = value

      set = classmethod(set)

      def println(cls):
          print cls.m, # To make this example clearer, dont print newline

      println = classmethod(println)

class B(X):
      def __init__(self):
          self.set(5)

class C(X):
      def __init__(self):
          self.set(6)

b = B()
c = C()
b.println()
c.println()
print

b1, b2 = B(), B()
b1.println()
b2.println()
print

c1, c2 = C(), C()
c1.println()
c2.println()
print

b2.m = 10

b.println()
c.println()
b1.println()
b2.println()
c1.println()
c2.println()
B.println()
C.println()
print 

print b2.m            # This is b2's new local variable - not the B static variable B.X.m
print b2.__class__.m  # This is the static variable

Code: Select all

5 6
5 5
6 6
5 6 5 5 6 6 5 6
10
5
but unfortunately, private and constants dont exist - even static is loosely-typed.
...am still trying to figure out how the hell to write code in python.

Posted: Sun Aug 26, 2007 11:11 am
by kaszu
Only solution what i can think of is to have for each child class separate static member.

Code: Select all

class X
 {
        public function set($value)
         {
                $this->setChildValue($value);
         }

        public function println()
         {
                echo $this->getChildValue();
                echo "\n";
         }
 }

class B extends X
 {
        protected static $b;

        public function __construct()
        {
                self::$b = 6;
        }

        public static function setChildValue($value)
        {
                self::$b = $value;
        }

        public static function getChildValue()
        {
                return self::$b;
        }
 }
class C extends X
 {
        protected static $c;

        public function __construct()
         {
                self::$c = 5;
         }

        public static function setChildValue($value)
        {
                self::$c = $value;
        }
        public static function getChildValue()
        {
                return self::$c;
        }
 }

Posted: Sun Aug 26, 2007 12:10 pm
by Chris Corbyn
Maybe you shouldn't be using static fields for this? There may be a better way of putting this together. Perhaps you could give us an example of where it will be used? :)

Posted: Sun Aug 26, 2007 12:30 pm
by anjanesh
Damn ! How come I didnt think of accessing a child's method instead of the child's member directly ?
I guess I was always thinking in terms of self::method() instead of $this->method() ... and parent:: and hence was looking for a child:: like solution.
Thanks kaszu.
Maybe you shouldn't be using static fields for this?
I have a base class and all derived classes have a single common property.
And a common function like println which is just going to print a variable - I could've put that in all the derived classes but thats a whole lot of copy-paste - so I was looking for something in the base class that could access the static member of the derived classes.

Re: Overriding static members or Accessing child members

Posted: Sat Feb 23, 2008 7:13 am
by anjanesh
Update : As of PHP 5.3, you can access a child's static member from a method defined in the parent using static:: - Late Static Binding.

Code: Select all

class X
 {
        public static $m = 2;
 
        public function println()
         {
                echo self::$m . "\n";   # Current which is Parent's
                echo static::$m . "\n"; # Child's
         }
 }
 
class B extends X
 {
        public static $m = 5;
 }
 
class C extends X
 {
        public static $m = 6;
 }
 
$b = new B();
$c = new C();
 
$b->println();
$c->println();