xinteractx Posted December 18, 2007 Share Posted December 18, 2007 Hello All I am new to the forum, i just joined.. looking forward to a good place to meet other pro php developers. My OO knowldge is very limited, I been doing mostly php procedural coding. I am starting my next project here at work and I trying to do this one with a CLASS Objects to see how it all works out. I am getting some errors, and Its prob because I dont know OO and PHP. I read a bunch of documents on it, however there are still little questions I cant seem to find exact answers for. Below is the code I wrote so far to connect to the database. <?php //The DB_OBJECT simple creates the connection to the database //this object must be called first. class DB_OBJECT { // pirvate user object data var $db_username; // username for the db var $db_password; // password for the db var $db_server_name; // the db server_name var $db_db_name; // the db database name var $db_returned_status; //true if connection successfull, if false returns error. var $db_primary_object; // used to store database connection object function connect($db_username,$db_password,$db_server_name,$db_db_name) { if (!this->$db_primary_object) { //create connection object. this->$db_primary_object = mysql_connect($db_server_name,$username,$password); if (!this->$db_primary_object) { this->$db_returned_status==mysql_error(); } else { this->$db_returned_status=true; mysql_select_db($db_db_name,$db_primary_object); } } return $this->$db_returned_status; } } ?> But I keep getting a error, and I think it has something to do with the this->$db_primary_object part of the code. maybe some hidden rules with php and OO i am not getting ? Parse error: parse error, unexpected T_OBJECT_OPERATOR in \htdocs\sales\include\db.php on line 21 here is the basic code i am using to test the object. <?php include ($_SERVER['DOCUMENT_ROOT'].'/sales/include/db.php'); // DB Connection object // connect to database using custom db object $database_obj=new DB_OBJECT("username","password","localhost","call_center"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/ Share on other sites More sharing options...
xinteractx Posted December 19, 2007 Author Share Posted December 19, 2007 Hello All, I been fighting with this code all day yesterday and for the life of me it is not working... Anyone provide any help please with caffeine ontop. Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-418403 Share on other sites More sharing options...
Pancake Posted December 19, 2007 Share Posted December 19, 2007 The correct syntax to run your code: <?php $database_obj = new DB_OBJECT(); $database_obj->connect('username', 'password', 'localhost', 'call_center'); ?> And a little nicer way to make your code: (Should Work) function connect($db_username,$db_password,$db_server_name,$db_db_name) { if (this->$db_primary_object = mysql_connect($db_server_name,$username,$password)) { //The way you originally had it, it would never connect since $this->db_primary_object was NULL in your ORIGINAL script :\ //create connection object. return true; mysql_select_db($db_db_name,$db_primary_object); } else { return mysql_error(); } } Smaller and more efficient! Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-418556 Share on other sites More sharing options...
emehrkay Posted December 19, 2007 Share Posted December 19, 2007 I assume that you are running php4. The problem doesnt appear to be with your class but the way you are calling it. You class has no constructor (a function that is run when the class is initialized). In php4 (possible in 5) to create a constructor, you just name that method the same name as the class. You can fix this one of two ways; rename connect to DB_OBJECT or call the connect method passing in the params $database_obj = new DB_OBJECT(); $database_obj->connect("username","password","localhost","call_center"); //connect called Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-418565 Share on other sites More sharing options...
Pancake Posted December 19, 2007 Share Posted December 19, 2007 Yea, if you are using PHP 5, rename the function "connect" with "__construct" So it would be: function __construct($db_username, $db_password, $db_server_name, $db_db_name) { And to execute: $atabase_obj = new DB_OBJECT('username', 'password', 'localhost', 'call_center'); I had plenty of trouble grasping the concept of OOP in php when I started :\ __construct runs when the class is called. Remember, __construct works only in PHP5 or above!! Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-418573 Share on other sites More sharing options...
Aeglos Posted December 22, 2007 Share Posted December 22, 2007 If I may add my 2 cents. I found out two errors in your code that might also be the problem. First: //create connection object. this->$db_primary_object = mysql_connect($db_server_name,$username,$password); The marked variables are not declared anywhere in your class or method. They should be $db_username and $db_password Second: if (!this->$db_primary_object) { this->$db_returned_status == mysql_error(); } That's a logical comparison symbol (double ='s) and not an assignment. Your are basicaly comparing your class member (variable) with the mysql_error() function. It should read: if (!this->$db_primary_object) { this->$db_returned_status = mysql_error(); } Hope it helps. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-421283 Share on other sites More sharing options...
coder_ Posted December 23, 2007 Share Posted December 23, 2007 can't belive that nobody noticed this error.. should it be like this: $this->db_primary_object = mysql_connect(....); Quote Link to comment https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/#findComment-421921 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.