Overriding static members or Accessing child members
Posted: Tue Aug 21, 2007 7:38 am
Im looking for a solution where an inherited class has a static member, but should be accessible in a method in the parent class.
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::).
Is there a way to make either possible ? If not php, does any language support accessing child members from parent ?
Thanks
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();
?>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);
}
}Thanks