www.webdeveloper.com
Results 1 to 5 of 5

Thread: OOP vs Procedural, question about function names

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    OOP vs Procedural, question about function names

    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();

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    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

    eBookworm.us

  3. #3
    Join Date
    Apr 2012
    Posts
    55
    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.
    joi JavaScript Framework - the sunnier side of JavaScript.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    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

    eBookworm.us

  5. #5
    Join Date
    Mar 2010
    Posts
    281
    thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles