Jump to content

OO Coding Explaination Required


OAFC_Rob

Recommended Posts

Hey, I'm finally getting around to teaching myself OO PHP, I been working on a database connetcion class and based it on a tutorial I found on the net. However, it doesn't explain it too well can anyone help me please.

 

I know what is going on right up until " $this->_database_connection" and "$this->_database_connection_select", at first I had the coding as "$this->databaseConnection = mysql_pconnect($this->databaseHostname, $this->databaseUsername, $this->databasePassword) or trigger_error(mysql_error(),E_USER_ERROR);" because I thought it was saysing the this function database connection should connect to the mysql database using these variables. But it didn't seem to working, got it fixed now using after looking at the tutorial but there is no explaination to why this works and mine didn't.

 

I understand that databaseHostname for example is being defined by the object, on the third bit of coding. Actually thinking about it, have I also defined in the first bit of coding.

 

I haven't included the SQL statement that retrives data, just so you know.

 

If anyone can help I will greatly appericate it  :D

 

<?php 
    # CONNECTION DETAILS THAT ARE THEN PASSED THROUGH TO "dbConnecxt"
    $databaseHostname = "localhost";
    $databaseUsername = "username";
    $databasePassword = "password";
    $databaseName = "databasename"; 
?>

 

<?php
    require_once ("config/config.php");
    
    class databaseConnectionClass
        {
     
           
            public $databaseHostname;
            public $databaseUsername;
            public $databasePassword;
            public $databaseName; 
     
            # MAIN CONNECTION TO THE DATABASE, PASSING THE 
            public function databaseConnection($objDatabaseConnect)
            {
                $this->_database_connection = mysql_pconnect($this->databaseHostname, $this->databaseUsername, $this->databasePassword) or trigger_error(mysql_error(),E_USER_ERROR);
               
                return $this->_database_connection;
            }

            # SELECTS THE DATABASE WE WANT
            public function databaseConnectionSelect()
            {
                $this->_database_connection_select = mysql_select_db($this->databaseName, $this->_database_connection);
                
                return $this->_database_connection_select;
            }
     
            # CALL ALL THE DATABASE CONNECTION OBJECTS
            public function databaseConnectionProcess($objDatabaseConnect)
            {
                $objDatabaseConnect->databaseConnection($objDatabaseConnect);
                $objDatabaseConnect->databaseConnectionSelect($objDatabaseConnect);
            }
     
            # BUILDS A OBJECT METHOD 
            public function databaseConnectionMain($objDatabaseConnect)
            {
                $objDatabaseConnect->databaseConnectionProcess($objDatabaseConnect);
            }
     
        }
    ?>

 

<?php
        #THIS CLASS CREATES AND OBJECT, WHICHS SETS THE OBJECT TO EQUAL THE INFO FROM "dbConnectClass.php"
        require_once ("dbConnectClass.php");
         
        $objDatabaseConnect = new databaseConnectionClass();
        $objDatabaseConnect->databaseHostname = $databaseHostname;
        $objDatabaseConnect->databaseUsername = $databaseUsername;
        $objDatabaseConnect->databasePassword = $databasePassword;
        $objDatabaseConnect->databaseName = $databaseName;
     
        $objDatabaseConnect->databaseConnectionMain($objDatabaseConnect);
?>

Link to comment
Share on other sites

I'm also not sure why your previous version didn't work, and it's difficult to tell without seeing it.

 

BTW you do not need to pass $objDatabaseConnect to databaseConnectionMain, because it can use $this.  And there's no need to store the return value of the database select in a variable, since that value will never be used again.  Only the connection will be being used later.

Link to comment
Share on other sites

BTW you do not need to pass $objDatabaseConnect to databaseConnectionMain, because it can use $this.  And there's no need to store the return value of the database select in a variable, since that value will never be used again.  Only the connection will be being used later.

I'm assuming the returns are to check for connection errors. Maybe the tutorial needs the return value at a later stage to ensure the connection was made.?

 

This is how I normally handle MySQL connections https://github.com/ttocskcaj/The-Forum-Framework/blob/master/libs/mysql.php

Link to comment
Share on other sites

Returning the value makes sense, but storing it in an object property doesn't make sense to me.  If I wanted other code to know if the database connection has been made I would add a property or method with a name like isConnected().

 

Back to your original post - what exactly is it you want help with?  If you want to know why your previous code didn't work, I'll need to see your previous code.

Link to comment
Share on other sites

Here is the previous coding, nothing else has changed on the other pages.

 

 

<?php
    require_once ("config/config.php");
    
    class databaseConnectionClass
        {
     
           
            public $databaseHostname;
            public $databaseUsername;
            public $databasePassword;
            public $databaseName; 
     
            # MAIN CONNECTION TO THE DATABASE, PASSING THE 
            public function databaseConnection($objDatabaseConnect)
            {
               #not to sure what is happening here at the moment
                $this->databaseConnection = mysql_pconnect($this->databaseHostname, $this->databaseUsername, $this->databasePassword) or trigger_error(mysql_error(),E_USER_ERROR);
               
                return $this->databaseConnection;
            }

            # SELECTS THE DATABASE WE WANT
            public function databaseConnectionSelect()
            {
                $this->databaseConnectionSelect = mysql_select_db($this->databaseName, $this->_database_connection);
                
                return $this->databaseConnectionSelect;
            }
     
            # CALL ALL THE DATABASE CONNECTION OBJECTS
            public function databaseConnectionProcess($objDatabaseConnect)
            {
                $objDatabaseConnect->databaseConnection($objDatabaseConnect);
                $objDatabaseConnect->databaseConnectionSelect($objDatabaseConnect);
            }
     
            # BUILDS A OBJECT METHOD 
            public function databaseConnectionMain($objDatabaseConnect)
            {
                $objDatabaseConnect->databaseConnectionProcess($objDatabaseConnect);
            }
     
        }
    ?>

 

I actually I think i ve foudn the problem, it i'm calling a $this->_database_connection which doesn't exist. I've just reuploaded it and try it, working fine now  :confused:

 

So am I do the following,

 

1. Defining some properties / variables.

2. creating a function called databaseConnection, which is getting info from an object of the class called $objDatabaseConnect.

3. I'm then going this temp thing called $this->databaseConnection or when i fist posted $this->_database_conection is equal to a mysql persistant connection.

4. The values for the connection are $this->databaseUsername, which are temp variables that are being set by the object on the other page.

$objDatabaseConnect->databaseHostname = $databaseHostname; (Object set databaseHostname to the public property/variable $databaseHostname)

5. The rest of the functions are pretty much useless apart from the select one correct and are there to make things a little eaiser on the coders brain.

 

Is that how it is working?

 

Also how would be best to do a statement that says if local database then do this or it will be live. How do you define which is local and which is live?

 

Cheers people  :)

Link to comment
Share on other sites

That sounds about right.. though I wouldn't call those "temp" variables.  You may well use them later, for example if you wanted to know what details were used to open the database connection.

 

There's a lot of possibilities about how to implement a choice between local and live database.  IMHO the best is if your script can detect where it is and choose the right database.  There's no "Best" way to do it though..

 

Regarding #2, you still should remove $objDatabaseConnect from the databaseConnection function arguments.  It doesn't need any arguments.  It knows which object to use because you called it from $objDatabaseConnect already, and it uses $this to access $objDatabaseConnect.

Link to comment
Share on other sites

  • 5 weeks later...
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.