Page 1 of 1

Class as member of class??

Posted: Fri May 07, 2010 7:57 am
by Dusty Ghost
Hey guys I'm trying to create an instance of a class inside another class. I know that inheritance is normally a good idea, but in this case I'm not so sure.
Here is the code:

Code: Select all

<?php
require_once('Class_SQL_Manager.php');

class User_Access{
	private $sql = new SQL_Manager();
	private $site_cookie;
	
	function get_cookie()
	{
		$this->sql->init();
		$this->site_cookie = $this->sql->get_array("Select Str_Value from control where Key_Value = 'COOKIE_NAME'");
		printf($this->site_cookie['Str_Value']);
		$this->sql->close_SQL();
	}
}
?>
This throws the following error:

Code: Select all

Parse error: syntax error, unexpected T_NEW in C:\XAMPP\xampp\htdocs\site\include\Classes\Class_User_Access.php  on line 5
Thanks in advance for anyone's help! :D

Re: Class as member of class??

Posted: Fri May 07, 2010 9:30 am
by yacahuma
I usually code by trial an error, so not sure if you can create an instance like that
what you can do

Code: Select all

class User_Access{
        private $sql;

        function __construct()
       {

 $this->sql= new SQL_Manager();
}


Re: Class as member of class??

Posted: Fri May 07, 2010 10:01 am
by Dusty Ghost
Ah! yes of course!!

The members of a class are the definitions not actual instances until the class is in an instance itself, doh! that's why I can't use the 'new' keyword yet as the class is being defined and not used yet! :banghead:

Oh well! you live and learn!! Thank you! :D