Hello there, I need something like this: class ParentClass { function blah() { echo $value; } } class ChildClass extends ParentClass { public $value = 'whatever'; } ChildClass::blah(); Code (markup): Gives me an error... How to pass a variable from ChildClass to it's parent? (called statical)
class Parental{ static $value="not set"; static function blah(){ echo "I am a " . __CLASS__ ."\n" ; echo "A value is: [" . self::$value ."]\n"; } function say(){ echo "Called method:[ " . __METHOD__ ."]\n"; } function icho($msg){ echo "echoing $msg from " . __METHOD__ ."\n"; } } class Chieldish extends Parental { function __construct($someValue){ self::$value = $someValue; } function icho($msg){ echo "calling a parent method from " . __METHOD__ ."\n"; parent::icho($msg); } } Chieldish::blah(); $ch = new Chieldish("SEO"); Chieldish::blah(); $ch->say(); $ch->icho("Hello World"); PHP: Output I am a Parental A value is: [not set] I am a Parental A value is: [SEO] //after construct of child class a value is set to 'SEO' Called method:[ Parental::say] calling a parent method from Chieldish::icho echoing Hello World from Parental::icho Code (markup): So if you need to use SOME_CLASS::METHOD(); you need to declare that method as "static" and any variables that are used in static methods must be static as well, otherwise you need to make an object of a class with "new" and call it with "->"
<?php class ParentClass { var $value; function blah() { echo $this->value; } function setblah($value){ $this->value = $value; } } class ChildClass extends ParentClass{} $my = new ChildClass; $my->setblah('whatever'); $my->blah(); ?> PHP: Peace,
Sure, like this it's working, but the problem is, I need to call ChildClass::blah(); and not $c = new ChildClass; $c->blah(); So I want to set a variable in the CODE of ChildClass which will be available at ParentClass when called like ChildClass::blah(); Imagine this would work (currently gives error "Undefined variable: my_value"): <?php class ParentClass { function blah() { echo $my_value; } } class ChildClass extends ParentClass { static $my_value = "THIS IS CHILD"; } ChildClass::blah(); Code (markup):
what the hell is all this? how much time do you need to learn php?and any ebook or website u wud like to share?
COULD try using SESSION variables? Or global variables...Because you can't do what you want the way you are. There is no way (I am aware of) to change a variable that doesn't exist yet. When you make the class, or call it, thats when the var is created. It is all memory allocation stuff...SESSIONS or $_GLOBAL variables might be what you need... If I am wrong, I will admit it, but currently I know of no way to do this.
hm didn't know this can be possible at all , thats cool I mean totally different way of programming can be unleashed