Home  >  Article  >  Backend Development  >  Benefits of using objects in database_PHP Tutorial

Benefits of using objects in database_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:44950browse

We all know how to get the rows (records) we need from Mysql, read the data, and then store and store some changes. It's obvious and straightforward, and there's no beating around the bush behind the process. However, when we use object-oriented programming (OOP) to manage the data in our database, this process needs to be greatly improved. This article will give a brief description of how to design an object-oriented approach to manage database records. All the internal logical relationships in your data will be encapsulated into a very organized record object, which can provide a specialized (specific) validation code system, transformation and data processing. With the release of Zend Engine 2 and PHP 5, PHP developers will have more powerful object-oriented tools to assist their work, which will make this process (object-oriented database management) more attractive.

Listed below are some of the advantages of using objects to describe your database:
Accessor methods will give you complete control over the reading and writing of properties
Each record and attribute (operation) at each level has a confirmation process
Intelligently obtain objects from relational tables
Reused logic means all data interactions go through the same codebase, which makes maintenance easier
The code is simple, because the internal logic of different records is already included in their respective classes, instead of cumbersome library (lib) files
There will be less chance of errors when writing code and SQL queries by hand

Accessor methods

The access method is to assign values ​​to the variables of the instance through the class. For example, I have a class called User and an instance $username. I will write such access methods (functions). User->username() and User->setUsername() are used to return and give Instance assignment.

class User {
var $username;

function username() {
return $this->username;
}

function setUsername($newUsername) {
$this->username = $newUsername;
}
}
?>

There are good reasons for writing "extraordinary code" like this. It will give developers more flexibility from the tedious work of changing classes because the process will not require additional PHP code that uses classes. Let's take a look at the more complete and reliable User class below.
The variable $username will no longer exist, and everything will be integrated into the array $_data
If username is empty, the username() function will provide a default (default) value to it
The setUsername() process will confirm whether the username conforms to the standard format (such as word length, etc.) before accepting the value

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'); // PHP5 only
}
return true;
}
}
?>

Obviously, this is very helpful for us to control the data of access objects. If a programmer already accesses username information directly, the above code changes will break his code. However we can use the accessor methods (of the class), as commented in the code above, to add validation functionality without changing anything else. Pay attention to the verification of username (in the example, it cannot exceed 12 bytes) and the code is independent of the setUsername() method. The process from validation to storage to database is a breeze. Moreover, this is a very good rule of thumb. The less a method or class needs to do, the greater the chance it will be reused. This is more obvious when you start writing a subclass, if you need a subclass, and you want to skip (ignore) some special details in the parent class method (behavior), if the method (for this detail) is small And it is delicate, (modifying it) is only a momentary process, and if this method is very bloated and serves multiple purposes, you may end up frustrated by copying a large amount of code in the subclass.

For example, if Admin is a subclass of User class. We may have different, relatively harsh password verification methods for Adamin users. It's best to go over the validation method of the parent class and the entire setUsername() method (overridden in the child class).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631386.htmlTechArticleWe all know how to get the rows (records) we need from Mysql, read the data, and then store and store some changes . It’s obvious and straightforward, and there’s no beating around the bush behind the process...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn