Crew-Portal Posted August 23, 2009 Share Posted August 23, 2009 <?php class Site{ // some stuff here. } class Connection extends Site{ private $database; private $host; private $username; private $password; private function Connect(){ $connection = @mysql_connect("$host", "$username", "$password") or die("Couldn't connect."); $db = @mysql_select_db($$database, $connection) or die("Couldn't select database."); } function Connection($db,$host,$user,$pass){ this->$database = $db; this->$host = $host; this->$username = $user; this->$password = $pass; Connect(); } } Site s = new Connection("bla","bla","bla","bla"); ?> It seems like it should work, However I end up getting this error: Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\lib\config.php on line 18 Any suggestions? Link to comment https://forums.phpfreaks.com/topic/171470-solved-first-time-using-classes-give-me-a-break/ Share on other sites More sharing options...
play_ Posted August 23, 2009 Share Posted August 23, 2009 '$this', not 'this' Site s = new Connection("bla","bla","bla","bla"); is also wrong. $conn = new Connection("bla","bla","bla","bla"); You might wanna look into constructors: http://us2.php.net/__construct Why does the Connection class extends site class? <?php class Connection { function __construct($host, $username, $password, $database) { // This function gets called whenever you instantiate an object(example, $con = new Connection($u, $p, $h, $d); ) $this->host = $host; $this->username = $username; $this->password = $password; $this->database = $database; // Call the function below. $this->Connect(); } private function Connect(){ $this->connection = @mysql_connect($this->host, $this->username, $this->password) or die("Couldn't connect."); $db = @mysql_select_db($this->database, $this->connection) or die("Couldn't select database."); } } $conn = new Connection("bla","bla","bla","bla"); ?> Link to comment https://forums.phpfreaks.com/topic/171470-solved-first-time-using-classes-give-me-a-break/#findComment-904249 Share on other sites More sharing options...
Crew-Portal Posted August 23, 2009 Author Share Posted August 23, 2009 I love you. Thanks alot man, means a ton It works beautifully! Link to comment https://forums.phpfreaks.com/topic/171470-solved-first-time-using-classes-give-me-a-break/#findComment-904258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.