vr4lyfe Posted February 9, 2007 Share Posted February 9, 2007 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! Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 Does your class have a variable called $connection? $this->connection does not use the $connection variable. In fact, it's quite gibberish to PHP. $this->$connection is what you want. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 $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. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 $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 $. Quote Link to comment Share on other sites More sharing options...
vr4lyfe Posted February 9, 2007 Author Share Posted February 9, 2007 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; } } } Quote Link to comment Share on other sites More sharing options...
vr4lyfe Posted February 9, 2007 Author Share Posted February 9, 2007 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() { } } Quote Link to comment Share on other sites More sharing options...
vr4lyfe Posted February 9, 2007 Author Share Posted February 9, 2007 Those are the two classes in question. I am still nub to OO as this is my first attempt at OO PHP. =] The code in questions is in the constructor of the Session class. Thanks for the help guys PS, I am using PHP Version 5.1.6 Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
vr4lyfe Posted February 9, 2007 Author Share Posted February 9, 2007 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 !! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.