son.of.the.morning Posted November 24, 2011 Share Posted November 24, 2011 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/251748-oo-php-help/ Share on other sites More sharing options...
son.of.the.morning Posted November 24, 2011 Author Share Posted November 24, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/251748-oo-php-help/#findComment-1291002 Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 -> 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; Quote Link to comment https://forums.phpfreaks.com/topic/251748-oo-php-help/#findComment-1291005 Share on other sites More sharing options...
son.of.the.morning Posted November 24, 2011 Author Share Posted November 24, 2011 This helps loads man, thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/251748-oo-php-help/#findComment-1291016 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.