We all know how to get the rows (records) We need from MySQL, read the data, and then access some of the changes. Obviously it's also straightforward, and there's nothing to beat around this process. However, when we use object-oriented programming (OOP) to manage the data in our database, the process needs to be greatly improved. This article will make a simple description of how to design an object-facing way to manage the records of a database. All of the internal logic of your data will be encapsulated into a very structured record object that can provide a dedicated (single-minded) validation code system, transformation, and data processing. With the release of Zend Engine2 and PHP5, PHP developers will have more powerful tools to support their work, which will make this process (facing the Object Management database) more appealing.
Here's a list of the benefits of using objects to describe your database:
1, access method (accessor methods) will enable you to read and write the properties of the process to complete control
2, each level of records and attributes (operations) have a confirmation process
3, from the relational table in the intelligent acquisition of objects
4. A reusable logical approach means that all data interactions have to go through the same basic code (CODEBASE), which makes maintenance easier
5, the code is simple, because the internal logic of different records are already contained in their respective classes (class), rather than the cumbersome library (Lib) file
6. There will be less chance of error when writing code and SQL query statements manually
Access method (accessor methods)
The access method is to assign a value to a variable of a class to an instance (instance). One example, I have a class called User, and there is an instance of $username, I'll write this accessor method (function), User->username () and User->setusername () to return and assign values to the instance.
<?php
class User {
var $username;
function username() {
return $this->username;
}
function setUsername($newUsername) {
$this->username = $newUsername;
}
}
?>
Here's a good reason for us to write this "special code". It will make it more flexible for developers to change the tedious work of classes, because this process will not require other PHP code that uses the class. Let's take a look at this more complete and trustworthy user class below.
The variable $username will no longer exist, and everything is integrated into the array $_data.
If the username is empty, the username () function will provide a default (default) value to it
The Setusername () procedure confirms that the username is in a standard format (such as word length, etc.) before accepting the value.
<?php
class User {
var $_data = array(); // associative array containing all the attributes for the User
function username() {
return !empty($this->_data['username']) ? $this->_data['username'] : '(no name!)';
}
function setUsername($newUsername) {
if ($this->validateUsername($newUsername)) {
$this->_data['username'] = $newUsername;
}
}
function validateUsername(&$someName) {
if (strlen($someName) > 12) {
throw new Exception('Your username is too long