class structure.

From: Date: Thu, 20 Jan 2005 02:30:28 +0000
Subject: class structure.
Groups: php.general 
Request: Send a blank email to php-general+get-206809@lists.php.net to get a copy of this message
Hi there, I am pretty new to writing classes (and pretty new to PHP itself), but I was wondering what was the best format for constructing classes. Now for example, i have written 2 versions of a class that accomplish the exact same thing. And I was just wondering if there are any advantages to either, or even if one was formatted more so to standards. Just wanting to learn it the proper way from the get go. My classes are formatted for use in flash remoting (hence the methodTable stuff - you can ignore it). I guess my question refers more towards the declaration of properties, etc. [ VERSION 1 ] <?php class Sql { // Set properties //DB info var $hostname_local = "localhost"; var $database_local = "mydb"; var $username_local = "root"; var $password_local = "password"; var $local; var $query_rs_biz; var $rs_biz; function Sql() { $this->methodTable = array( "getRecords" => array( "description" => "return a SQL record", "access" => "remote", "arguments" => array("") ) ); } //SQL //Create method to retreive records function getRecords() { $this->local = mysql_pconnect($this->hostname_local, $this->username_local, $this->password_local) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($this->database_local, $this->local); $this->query_rs_biz = "SELECT * FROM directory ORDER BY Business ASC"; $this->rs_biz = mysql_query($this->query_rs_biz, $this->local) or die(mysql_error()); //return $this->row_rs_biz; return $this->rs_biz; } } ?> [ VERSION 2 ] <?php class Sql2 { // Set properties //DB info var $hostname_local = "localhost"; var $database_local = "Blisting"; var $username_local = "root"; var $password_local = "URAredneck"; function Sql2() { $this->methodTable = array( "getRecords" => array( "description" => "return a SQL record", "access" => "remote", "arguments" => array(), "returns" => "sql records" ), "getRecCount" => array( "description" => "return the row count from the getRecords method", "access" => "remote", "arguments" => array(), "returns" => "sql row count" ) ); } //SQL //Create method to retreive records function getRecords() { $local = mysql_pconnect($this->hostname_local, $this->username_local, $this->password_local) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($this->database_local, $local); $query_rs_biz = "SELECT * FROM directory ORDER BY pID ASC"; $rs_biz = mysql_query($query_rs_biz, $local) or die(mysql_error()); //return results; return $rs_biz; } } ?>

« previous php.general (#206809) next »