PHP Classes - Pass class to other class?

B

Bluewind

Guest
Problem has been SOLVED.

I have some classes, but I can't use the variables off one of them in the others.
The relevant code:
index.php
PHP:
//--------------------------------
// INIT other classes
//--------------------------------
$sfs->mysql            		=  new mysql_driver();
$sfs->mysql->sfs 			=& $sfs;
$sfs->mysql->connect();

mysql_class.php
PHP:
class mysql_driver {
	function __construct(){
	}

	 function connect(){
	 var_dump($sfs);
		$this->connection_id = @mysql_connect($sfs->INFO['sql_host'], $sfs->INFO['sql_user'], $sfs->INFO['sql_pass']);

		if (!$this->connection_id) {
			$this->fatal_error();
		}

		if (!mysql_select_db($sfs->INFO['sql_database'], $this->connection_id)) {
        	$this->fatal_error();
        }
	}
}
At the point of the var_dump() I don't have any variable called $sfs. Is
PHP:
$sfs->mysql->sfs =& $sfs;
impossible or do I have to catch the main class ($sfs) in the construct (if yes, please say how)?

Thanks in advance.
 

phyrex1an

Staff Member and irregular helper
Reaction score
446
PHP:
var_dump($this->sfs);

Object (not class) properties are accessed with the $this special variable.

Also, since mysql_driver have a dependency on $sfs then you should pass it thought the constructor.

PHP:
class mysql_driver {
    protected $sfs;
 
    function __construct($sfs){
         $this->sfs = $sfs;
    }
...
 
B

Bluewind

Guest
PHP:
var_dump($this->sfs);

Object (not class) properties are accessed with the $this special variable.

Also, since mysql_driver have a dependency on $sfs then you should pass it thought the constructor.
Haven't tried that yet because I just found out what I did wrong:
PHP:
mysql_connect($sfs->INFO['sql_host'],
must be
PHP:
mysql_connect($this->sfs->INFO['sql_host'],

Thanks anyway.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top