How to pass from ChildClass to ParentClass??

Discussion in 'PHP' started by paul0988, Sep 23, 2008.

  1. #1
    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)
     
    paul0988, Sep 23, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
  3. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    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 "->"
     
    classic, Sep 23, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    
    <?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,
     
    Barti1987, Sep 23, 2008 IP
  5. paul0988

    paul0988 Guest

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    paul0988, Sep 26, 2008 IP
  6. infinity0

    infinity0 Peon

    Messages:
    248
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what the hell is all this?:confused:
    how much time do you need to learn php?and any ebook or website u wud like to share?
     
    infinity0, Sep 26, 2008 IP
  7. paul0988

    paul0988 Guest

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes, that's exactly why I'm asking ;)
     
    paul0988, Sep 27, 2008 IP
  8. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    NatalicWolf, Sep 27, 2008 IP
  9. paul0988

    paul0988 Guest

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi,

    ok I see.

    Thank you very much :)
     
    paul0988, Sep 28, 2008 IP
  10. allx

    allx Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well, if you have the opportunity, upgrade to PHP 5.3 which will support late static binding.
     
    allx, Sep 29, 2008 IP
  11. paul0988

    paul0988 Guest

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hey, thank you very much!!!! That's exactly what I need
     
    paul0988, Oct 1, 2008 IP
  12. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    hm didn't know this can be possible at all , thats cool I mean totally different way of programming can be unleashed :)
     
    classic, Oct 2, 2008 IP