PHP program writing is divided into process-oriented and object-oriented. There is no difference in the implementation of the function, but in code writing is very different, the process-oriented code is very messy, difficult to manage, and object-oriented to encapsulate common functions as a class, so the code is much clearer.
Here is a small example to illustrate:
PHP Connection Database:
Process-oriented: $conn = mysql_connect (' Server name ', ' Database login name ', ' password ') or Die (' connection unsuccessful! ');
mysql_select_db (' Library name ', $conn) or Die (' database does not exist! ‘);
$queryid = mysql_query ("Select * from sort");
while ($rs = Mysql_fetch_assoc ($queryid)) {
echo $rs [' name '], ' <br> ';
Object-oriented: 1. Build a php file called db.php, which encapsulates the above code into a class:
Class DB {
Private $conn; Property
Private $queryid; Property
constructor function
Public Function db ($host, $dbuser, $DBPASSWD, $dbname) {
$this->conn = mysql_connect ($host, $dbuser, $dbpasswd) or die (' Connection not successful! ');
mysql_select_db ($dbname, $this->conn) or Die (' database does not exist! ‘);
}
Query method
Public Function Query ($sql) {
$this->queryid = mysql_query ($sql, $this->conn);
}
Public Function Fetch_assoc () {
Return Mysql_fetch_assoc ($this->queryid);
}
}
2. Instantiation in the a.php file, code usage becomes simple and easy to understand, code reusability
Inclode (' db.php ');
$db = new db (' Server name ', ' Database login ', ' password ', ' library name ');
$db->query ("select * from sort");
while ($rs = $db->fetch_assoc ()) {
echo $rs [' name '], ' <br> ';
}
PHP process-oriented and object-oriented