Analysis on the Data Object ing pattern of php design pattern _ php instance

Source: Internet
Author: User
There are many different design modes in php. Here we will introduce you to an uncommon data ing mode, if you are interested, let's take a look at the various design modes in php. Here we will introduce you to an uncommon data ing mode, I hope this article will help you.

The data ing mode allows you to better organize your applications to interact with databases.

The data ing mode reduces the combination density between object attributes and the table fields that store them. The essence of the Data ing mode is a class, which maps or translates the attributes of the class or the corresponding fields from the method to the database, and vice versa.

The role of data ing is to be able to understand the information presented by both parties, and to control the access to information, such as based on the information stored in the data table

Re-create a domain object or update or delete related data in the data table using the information of the domain object.

There are multiple ways to store the ing between object-oriented code and database tables and fields. One possible method is to manually encode the ing relationship and store it in the Data ing Class.

Another optional method is to use the PHP array and encode it as the class itself. This class can also obtain data from external sources, such as INI or XML files.

The Data Object ing mode maps objects and data storage. operations on an object are mapped to operations on data storage.

Implement the Data Object ing mode in the code, implement an ORM class, and map complex SQL statements into object attributes. Object Relational ing (ORM)

Ha_cl table

Hacl. php

<?phpnamespace Baobab;class Hacl{public $id;public $haclname;public $haclcode;public $hacls;protected $db;function __construct($id){$this->db = new \Baobab\Database\Mysqli();$this->db->connect('127.0.0.1', 'root', '', 'test');$res = $this->db->query("select * from ha_cl where id = {$id}");$data = $res->fetch_assoc();$this->id = $data['ID'];$this->haclname = $data['ha_cl_name'];$this->haclcode = $data['ha_cl_code'];$this->hacls = $data['hacls'];}function __destruct(){$this->db->query("update ha_cl setha_cl_code = '{$this->haclcode}',ha_cl_name = '{$this->haclname}',hacls = '{$this->hacls}'where ID = {$this->id}limit 1");}}

Factory. php

<? Phpnamespace Baobab; class Factory {static function getHacl ($ id) {$ key = 'user _'. $ id; $ user = \ Baobab \ Register: get ($ key); // different table IDs indicate different objects if (! $ User) {$ user = new \ Baobab \ Hacl ($ id); \ Baobab \ Register: set ($ key, $ user);} return $ user ;}}

Register. php

<?phpnamespace Baobab;class Register{protected static $objects;static function set($alias, $object){self::$objects[$alias] = $object;}static function _unset($alias) {unset(self::$objects[$alias]);}static function get($name) {return self::$objects[$name];}}

Index. php

Class Page {function index () {$ hacl = Baobab \ Factory: getHacl (13); $ hacl-> haclname = 'test name'; $ this-> test (); echo 'OK';} function test () {$ hacl = Baobab \ Factory: getHacl (13); $ hacl-> hacls = 'test content ';}} $ page = new Page (); $ page-> index ();

In factory mode, object Hacl is created multiple times, which wastes resources. If an object is passed as a parameter, on the one hand, additional costs are incurred, in addition, if this object is used in many places, errors may easily occur. Therefore, use the registration tree mode in factory mode to solve this problem.

The above content introduces the Data Object ing mode of the php design mode. I hope it will be helpful to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.