Jump to content

Recommended Posts

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");

 

?>

Link to comment
https://forums.phpfreaks.com/topic/82195-problems-getting-started-with-oo-php/
Share on other sites

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!

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

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!!

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.

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.