Home Backend Development PHP Tutorial Benefits of using objects in database_PHP Tutorial

Benefits of using objects in database_PHP Tutorial

Jul 13, 2016 pm 04:58 PM
mysql use exist benefit how object us data database of Know Obtain Record read

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.

<?php
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

<?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'); // 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 of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left!

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

See all articles