面向对象与面向过程有什么区别:
在mysqli的扩展库里,既支持面向对象又支持面向过程,但是老师告诉我,最好使用面向对象的方式编程,对于新手的我来说,很是不理解。
在PHP文档里面,关于mysqli的介绍中,文档既写出了使用面向对象编程的实例,也写出了面向过程编程的实例,我比较了一下,发现两者没有多大区别。
举例说明一下,我们用语句从数据库里提取一段记录,
这是文档中mysqli::query中面向对象风格的实例
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);    /* free result set */
    $result->close();
}/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}$mysqli->close();
?> 这是文档中mysqli::query中面向过程风格的实例
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));    /* free result set */
    mysqli_free_result($result);
}/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!mysqli_query($link, "SET @a:='this will not work'")) {
        printf("Error: %s\n", mysqli_error($link));
    }
    mysqli_free_result($result);
}mysqli_close($link);
?> 我通过观察发现,不论是面向对象还是面向过程,数据库的查询只有三部
一、连接数据库,不同的是,面向对象中使用实例化mysqli类来连接,面向过程中使用mysqli_connect()函数连接
二、数据库查询,面向对象使用$mysqli::query(语句)查询,面向过程使用mysqli_query(语句)查询
三、关闭资源,面向对象使用$mysqli->close()关闭,面向过程使用mysqli_close()关闭
老师也说过mysqli的使用与原来很相似,但是对于新手的我来说,过程一样,只是用类实例化对象调用方法来代替原来的函数,不论怎么想都没有什么实际的优势,在网上搜索了很多,好多人会说面向对象更高效更利于维护之类的高深术语,对于新手的我来说,根本没区别。
其实就是想知道在具体开发,或者说编程中,面向对象编程与面向过程编程有多大区别?