search
HomeBackend DevelopmentPHP TutorialBenefits of using objects in database_PHP Tutorial
Benefits of using objects in database_PHP TutorialJul 13, 2016 pm 04:58 PM
mysqluseexistbenefithowobjectusdatadatabaseofKnowObtainRecordread

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
CS玩家的首选:推荐的电脑配置CS玩家的首选:推荐的电脑配置Jan 02, 2024 pm 04:26 PM

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

access数据库的结构层次是什么access数据库的结构层次是什么Aug 26, 2022 pm 04:45 PM

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool