Jump to content

[SOLVED] $this is confusing me - please help


vr4lyfe

Recommended Posts

dbLink is my db abstraction class.

This syntax work when I am using the class:

$connection = new dbLink("session");
$connection->query("sql goes here");

Now I am making another class, which uses and include_once() to integrate the db class and this syntax does not work:

$this->connection = new dbLink("session");
$this->connection->query("sql stuff goes here");

Can anyone help me with the proper syntax to make the second code fragment work?

Thanks!

Link to comment
Share on other sites

$this->connection is the proper way to access a class variable $connection, it isn't gibberish.

$this->$connection will look for a variable with the name that is the value of $connection.

 

IE:

$bar = 'foo';

$foo = 10;

 

echo $$bar; will print 10.

 

vr, I'm not sure you can just include the db class as a way to integrate it. If you post the two files we can try to help better.

Link to comment
Share on other sites

$this->connection is the proper way to access a class variable $connection, it isn't gibberish.

$this->$connection will look for a variable with the name that is the value of $connection.

 

Ugh, the pain of various OO systems.

 

Sorry, I originally learned OO with Java, and it took over my brain during my post. My natural assumption is that you'd use the variable's name (that is, with the $) as opposed to everything except the $.

Link to comment
Share on other sites

class dbLink {

	var $server;				# server address
	var $username;				# username to connect with
	var $password;				# password to connect with
	var $database;				# name of database to use
	var $connection;			# database connection object
	var $errors;				# array to contain all errors encountered

	# CONSTRUCTOR

	function __construct($table_to_use) {

		# If new connection does not specify which database to connect to, default to localhost.

		switch($table_to_use){ 
			default: 
				$mysql_server_ip = 'localhost'; 
				break;
		}

		$mysql_db_name = 'facetime';

		if($_COOKIE['power'] == 0){
			# REGISTERED USER [2] || PAID USER [4] || RECRUITER [6]
			# connect to database as normal account
			$mysql_user 	= 'ft_user';
			$mysql_password = 'password';
		} else { 
			# GUESTS || 
			# connect to database as guest account with only SELECT priviledges
			$mysql_user 	= 'ft_guest';
			$mysql_password = 'password';
		}

		$this->connect($mysql_server_ip, $mysql_user, $mysql_password, $mysql_db_name);
	}

	# Creates connection to database

	function connect($server,$username,$password,$database){
		$this->server = $server;
		$this->username = $username;
		$this->password = $password;
		$this->database = $database;

		$this->connection = mysql_connect($this->server, $this->username, $this->password);

		if (!$this->connection) { $this->errors[] = mysql_error(); } 
			else {		
				if(!mysql_select_db($database, $this->connection)){	$this->errors[] = mysql_error(); }
			}
	}

	# Break connection to database

	function disconnect() {		}		

	# QUERIES

	# This function is the base wrapper to pass any sql statement (insert / delete / select)
	# Returns result object on success, or false on failure.

	function query($sql) {	if($result = mysql_query($sql, $this->connection)){		return $result;		} else { 	return false;	}	}

	# This function utilize the base db wrapper to execute select queries			
	# Return array of results on success to avoid user having to implement mysql_fetch calls.   Return false on failure.

	function select($sql) {
		if($result = $this->query($sql)){
			while($row = mysql_fetch_assoc($result)){	$result_array[] = $row;		}		
			return $result_array;
		} else {
			$this->errors[] = mysql_error();
			return false;
		}			
	}

}

Link to comment
Share on other sites

class Session {

	var $sessionID;				# 
	var $userID;				#
	var $userhash;				# 
	var $userpower;				# 
	var $time;					# 
	var $connection;			# stores a connection to the db once made with the connect function
	var $errors;				# array to contain all errors encountered

	# CONSTRUCTOR

	function __construct() {

		# if session cookie is available pull from session db

		# if session cookie is not available, log error return false
		if(isset($_COOKIE['session'])){
			$this->connect();
			$tmp = $this->$connection->select("SELECT * FROM session");
			print_r($tmp);				
		} else {
			$this->errors[] = "No sessions found";
		}

	}

	# Creates connection to database that contains the session table

	function connect(){
		if(!include_once('./includes/class_dbLink.php')){ $this->errors[] = "Failed to include required file for database connection."; }
			else {	$this->$connection = new dbLink("session"); }			
	}

	# Break connection to database

	function disconnect() {		}		


}

Link to comment
Share on other sites

Shouldn't you include_once the file outside that function? I'm not too sure, but I believe it wouldn't generate an error, but scope all of that class info to just within that function.

 

Try using the include_once within the class, but outside a function.

 

And wow, second person I've seen today using # for their comments.

Link to comment
Share on other sites

Try this:

move your include to the top, before the class declaration.

include_once('./includes/class_dbLink.php');

 

Then change the $this->$connections to $this->connection.

 

That should fix the problem.

 

Also, I don't believe include_once() will return a false value if the include fails - you might want to use require() instead of your if statement.

Link to comment
Share on other sites

omg .. i m so sorry i wasted everyone's time .. i feel incredibly stupid right now.

 

I assumed the statement was not working since it was supposed to output some db result.  Turns out I forgot that my db was empty.

 

But yes, this is the correct syntax.

 

$this->varname->functionname();

 

Thanks to everyone who replied.  Have a great Friday all !!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.