marcin_koss Posted December 31, 2009 Share Posted December 31, 2009 Hello people. I'm new to OO PHP and started with writing my first class that creates MySQL connection. I would like someone to look at it and check if I'm following best OO practices. In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows class mysql_query{ protected $_query; protected $_result; protected $_connect; private $_root = "root"; private $_pass = "pass"; private $_host = "localhost"; private $_dbname = "database"; public function __construct($q) { $this->_query = $q; $this->_connect = mysqli_connect($this->_host, $this->_root, $this->_pass, $this->_dbname); } public function getResult() { $this->_result = mysqli_query($this->_connect, $this->_query) or trigger_error("Query: $this->_query\n<br />MySQL Error: " . mysqli_error()); return $this->_result; mysqli_close($this->_connect); // close connection } public function fetchNum() { $numRow = mysqli_num_rows($this->_result); return $numRow; } public function fetchArray() { $dbArray = mysqli_fetch_array($this->_result); return $dbArray; } public function fetchRow() { $rows = mysqli_fetch_row($this->_result); return $rows; } } $query "some mysql query statement"; $db = new mysql_query($query); $db->getResult(); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/ Share on other sites More sharing options...
Daniel0 Posted December 31, 2009 Share Posted December 31, 2009 I'm sorry to break it for you, but your code is not OO just because you've made an object or a class. See: http://en.wikipedia.org/wiki/Object-oriented_programming Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986475 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 Calling a class mysql_query is also a very bad idea. In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows These are local variables in your methods, not members of your class. So yeah, that's ok. Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986531 Share on other sites More sharing options...
marcin_koss Posted December 31, 2009 Author Share Posted December 31, 2009 Calling a class mysql_query is also a very bad idea. In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows These are local variables in your methods, not members of your class. So yeah, that's ok. Thanks Mchl Hmmm, Is there some vulnerability issue by calling a class the same name as a built in function? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986557 Share on other sites More sharing options...
printf Posted December 31, 2009 Share Posted December 31, 2009 A query should a function within a database class, not a class it's self. Naming conversions are just as important coding logic. What I mean... If I see someone name a database class dbQuery or some other name that should purely only be a function within database class I tend to know that the coding logic will not be very sound. Sure everyone has their own coding style, but naming classes with names that will only confuse someone is simply illogical. A QUERY, QUERY_ERROR, etc, are logically member functions of a database class, not classes their self! Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986562 Share on other sites More sharing options...
marcin_koss Posted December 31, 2009 Author Share Posted December 31, 2009 A query should a function within a database class, not a class it's self. Naming conversions are just as important coding logic. What I mean... If I see someone name a database class dbQuery or some other name that should purely only be a function within database class I tend to know that the coding logic will not be very sound. Sure everyone has their own coding style, but naming classes with names that will only confuse someone is simply illogical. A QUERY, QUERY_ERROR, etc, are logically member functions of a database class, not classes their self! Thanks, that makes sense... Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986563 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 And it creates confusion. mysql_query is very common PHP function. While you, as the creator of the class will know what's going on, someone else might be surprised to see: $foo = new mysql_query($sql); Then they come here, post this piece of code, and ask US what the hell is going on, and we tell them, that mysql_query is a function, not a class, so they shouldn't try to instantiate it, but then they get error because there's no ext\mysql connection established etc., etc. Quote Link to comment https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/#findComment-986570 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.