Page 1 of 1

Constructor Variable Scope Help

Posted: Fri May 14, 2010 1:35 pm
by dhui
How do I use variables declared in a class constructor in the other class functions?

Code: Select all

        function __construct()
	{
		include('../../classes/Obj.class.php'); 
		$Obj = new Obj;
		$Obj->connect();	
	}

	function getSomething()
	{	
               //use $Obj here
	}

Re: Constructor Variable Scope Help

Posted: Fri May 14, 2010 1:39 pm
by Weirdan
By assigning them as a member of an object (via $this):

Code: Select all

include('../../classes/Obj.class.php'); 
class A {
        protected $obj = null;

        public function __construct()
        {   
                $this->obj = new Obj;
                $this->obj->connect();        
        }

        public function getSomething()
        {       
               //use $this->obj here
        }
}

Re: Constructor Variable Scope Help

Posted: Fri May 14, 2010 2:16 pm
by dhui
Thanks! Still getting used to php OOP variable scope and syntax :/