9three Posted February 12, 2009 Share Posted February 12, 2009 Hey, if I do the following: class mysql { private $host, $username, $password, $dbname; public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; } public function connect() { $this->link = mysql_connect($this->host, $this->username, $this->password); if (!is_resource($this->link)) Throw New Exception(mysql_error()); } public function select() { $this->select = mysql_select_db($this->dbname); } } If you notice, I'm not defining $this->link or $this->select. Is there a reason I should? Is this a security issue? Link to comment https://forums.phpfreaks.com/topic/144877-defining-this-variable/ Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 It should be throwing a syntax error, or atleast a warning. You should declare them. Link to comment https://forums.phpfreaks.com/topic/144877-defining-this-variable/#findComment-760276 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Alright thanks. Link to comment https://forums.phpfreaks.com/topic/144877-defining-this-variable/#findComment-760283 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Hey, just more questions that are coming to mind now: The more variables I declare at the beginning, the slower the application becomes? I'm thinking of two ways to generically pull information from a database with fetch a Assoc and fetch as an Array. public function fetchAssoc() { $this->fetchAssoc = mysql_fetch_assoc($this->query, $this->link); if (!is_resource($this->fetchAssoc)) Throw New Exception(mysql_error()); else { $array = array(); while ($field = $this->fetchAssoc) { $array[] = $field; } return $array; } } Or would it be better if I just did something like this: public function fetchAssoc() { $array = array(); while ($field = mysql_fetch_assoc($this->query, $this->link)) { $array[] = $field; } return $array; } What would be better for performance, and security? I know though that using the first method would actually benefit me in certain ways because of I'm error handling it properly. What do you guys think? Link to comment https://forums.phpfreaks.com/topic/144877-defining-this-variable/#findComment-760291 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 bump Link to comment https://forums.phpfreaks.com/topic/144877-defining-this-variable/#findComment-760327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.