I am trying to make an OO application and I am having trouble passing variables down the chain of command of classes. I am trying to create a var in the parent class and then use it again two sub-classes down. Apparantly there is some blockage between passing variables more than one level.

Take a look at this example of what I am trying to do:

<?php
class framework
{
	var $test = "Hello World";
}

class controller extends framework
{
}

class sub_controller extends controller
{
	function sub_controller()
	{
		echo($this->test); //This shows up blank (not set)

	$this->test = "Another Hello World";
}
}

$fw = new framework();
$c = new controller();
$sc = new sub_controller();

print($fw->test); //Outputs "Hello World", not "Another Hello World"
?>

My question is, how can I use the variables from the parent framework in the sub_controller without having to declare $fw as global inside every function I want to use it in. I know objects are supposed to work together, but apparantly I am missing the key to linking them together.

Also, and almost more importantly, how do I re-assign the value of a parent class variable from a sub-sub class?

Thanks!

    is it php 4 or php 5?

      Right now I am working with PHP5, but it should be compatible with PHP4.

        You should init the class data members(vars/properties) in constructors(best practise):

        class framework {
        
           var $test;
        
        function framework()
        {
           $this->test = 'Hello World';
        }
        };
        

        Run the base class constructor in sub class constructor:

        class controller extends framework {
        
        function controller()
        {
           parent::framework();
        }
        };
        //--------------------------------------------------------------------------------------
        class sub_controller extends controller {
        
        function sub_controller() 
        {
           parent::controller();
           echo($this->test); //This shows up 'Hello World'
           $this->test = "Another Hello World";
        } 
        };
        //--------------------------------------------------------------------------------------
        
        
        $fw = new framework(); 
        $c = new controller(); 
        $sc = new sub_controller(); 
        
        /*
           Outputs "Hello World" because '$fw' its an instance of 'framework'.
           The data members and functionality in 'framework' will not change because you
           use it as a base class for a object chain. 
        */
        print($fw->test);
        
        /*
           Outputs "Hello World".
           '$c' will have one data member($test) that is ineheritance from 'framework'
           and set to "Hello World" in the 'framework' constructor.
        */
        print($c->test);
        
        /*
           Outputs "Another Hello World".
           '$sc' will have one data member($test) that is ineheritance
           from 'controller' that again has it as a ineheritance from 'framework'.
           '$test' is overwritten to "Another Hello World" in the 'sub_controller' constructor.
        */
        print($sc->test);
        

          Thanks for that post. It was very helpful

            Write a Reply...