Jump to content

OO PHP help


son.of.the.morning

Recommended Posts

Hey guys, i was wondering if anyone can explain a bit of code for me. I am trying to learn OO PHP5 and am running a tutorial to create a db connect class but there is operators i havnt seen before.

I realy dont get this operator "->", also i cant seem to find anything online to explain it to me. Could anyone be kind enough to run me through this code a little more. Thank you.

 

 

//database connection class

class DatabaseConnectClass {

 

public $host_name;

public $username;

public $password;

public $db_name;

 

public function databaseConnection($objDatabaseConnect) {

$this->_databaseConnection =

mysql_pconnect($this->host_name,

  $this->username,

  $this->password)

or trigger_error(mysql_error(), E_USER_ERROR);

return $this->_database_connection;

}

 

//selection the database and open it

function databaseConnectionSelect() {

$this->databaseConnectionSelect =

mysql_select_db($this->db_name,

$this->_databaseConnection);

return $this->_database_connection_select;

}

 

//call all the database connection objects

function databaseConnectionProcess($objDatabaseConnect) {

$objDatabaseConnect->databaseConnection($objDatabaseConnect);

$objDatabaseConnect->databaseConnectionSelect($objDatabaseConnect);

}

 

//build main object method

public function databaseConnectionMain($objDatabaseConnect){

$objDatabaseConnect->databaseConnectionProcess($objDatabaseConnect);

}

 

}

Link to comment
Share on other sites

<?php 
//database connection class
class DatabaseConnectClass {

	public $host_name;
	public $username;
	public $password;
	public $db_name;

	public function databaseConnection($objDatabaseConnect) {
		$this->_databaseConnection = 
		mysql_pconnect($this->host_name,
					   $this->username,
					   $this->password)
		or trigger_error(mysql_error(),E_USER_ERROR);
		return $this->_database_connection;
	}

	//selection the database and open it
	function databaseConnectionSelect() {
		$this->databaseConnectionSelect =
		mysql_select_db($this->db_name,
						$this->_databaseConnection);
		return $this->_database_connection_select;
	}

	//call all the database connection objects
	function databaseConnectionProcess($objDatabaseConnect) {
		$objDatabaseConnect->databaseConnection($objDatabaseConnect);
		$objDatabaseConnect->databaseConnectionSelect($objDatabaseConnect);
	}

	//build main object method
	public function databaseConnectionMain($objDatabaseConnect){
		$objDatabaseConnect->databaseConnectionProcess($objDatabaseConnect);
	}

}

?>

 

Sorry i messed the code insert up

Link to comment
Share on other sites

-> means points to. (Go figure)

 

So when we say $this->username;

 

We are saying $this object that points to username.

 

So if we create an object of this class, like this:

$connection = new DatabaseConnectClass();

 

We would access it like $connection->username; meaning $connection Object that points to variable username.

 

There are different variable scopes. So, if I made a class like this:

 

class myClass() {
   public $globalScope = "global";

   function myClass() {
      $localScope = "test";
      $this->objectScope = "test2";
   }
}

 

You have a local variable $localScope, that you won't be able to access except from the method myClass(). You have a variable defined in the global scope of the class ($globalScope) which (i'd have to check this) is essentially the same as defining $this->objectScope = "test2"; You can access this variable anywhere in the class. Or, if its public, from the object itself like $myClass->objectScope;

Link to comment
Share on other sites

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.