I'm trying to get the hang OOP in PHP. in the tutorials i've noticed different function names for functions that seem to be doing the same thing. For Ex:
$query = "select * from pages where id = 1";
$result = $connection->query($query);
mysql_query ()(procedural) and query (in OOP)
theses are identical functions no?, if so, why the different naming?
same for:
mysql_fetch_array($result ) and $result->fetch_array();
Assuming that the OO versions are for the MySQLi extension, then yes, at least in a broad sense, they are doing the same thing. However, they are not interchangeable. You cannot use the MySQLi object-oriented query() to run the query and then use the MySQL mysql_fetch_array() to fetch a result row, as it knows nothing about a MySQLi Result object.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
As the PHP documentation describes, the reason for the two is just to give you the option to do whichever you want. Earlier versions of PHP/mysql functions only supported the procedural style. When OOP and mysqli were added, the procedural style was also kept for those familiar with using them. It really makes no difference which you use. If you prefer OOP, use the OOP ones. If you prefer procedural, use the procedural style. It's up to you.
Also note that the procedural versions of the MySQLi functions (e.g. mysqli_query()) are not interchangeable with the MySQL functions (e.g. mysql_query()).
My overall recommendation: learn OOPhp and use OOP wherever practical to do so, e.g. the MySQLi OOP implementation, or something like PDO as a (relatively) DBMS-agnostic interface.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks