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
https://forums.phpfreaks.com/topic/251748-oo-php-help/
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
https://forums.phpfreaks.com/topic/251748-oo-php-help/#findComment-1291002
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
https://forums.phpfreaks.com/topic/251748-oo-php-help/#findComment-1291005
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.