_php tutorial on object overloading techniques in PHP 5.0

Source: Internet
Author: User
Tags pear
Text/Zhu Xianzhong compilation

First, Introduction

Fortunately, PHP (which is now the mainstream development language) introduced the object overloading technique in 5.0. This article explores the possibility of overloading the method __call (), __set (), and __get (). After a brief introduction to the overloaded theory, we will pass two examples to the topic: the first example, the implementation of the continuous storage class, the second example, to find a way to implement dynamic Getter/setter.

Second, what is Object overloading?

When it comes to object overloading in PHP, which is now the mainstream development language, we want to differentiate between the two types:

• Method overloading

• Property Overloading

In the case of method overloading, we define a magic method __call (), which implements a general call to an undefined method in the corresponding class. This general method is called only if you want to access a method that is not defined in the class. In the absence of a method overload, the following example will cause PHP to display a fatal error message for the current mainstream development language: Call to undefined method Thiswillfail::bar () in/some/directory/ Example.php (as the current mainstream development language) on line 9 and abortion program execution:

Class Thiswillfail {
Public Function foo () {
Return "Hello world!";
}
}
$class = new Thiswillfail;
$class->bar ();
? >

With the help of method overloading, the code can capture this invocation and be able to handle it gracefully.

Property overloading is similar to method overloading. In this case, the class redirects the read/write operations (also called proxies) to the properties of the class, which are not explicitly defined in the class. The specialized methods here are __set () and __get (). Depending on the level of error Reporting, PHP (as the current mainstream development language) The translator typically accesses an undefined attribute, or emits a notification, or defers and potentially defines the variable. If you use property overloading, the translator can invoke __set () when setting an undefined property, and call __get () when accessing an undefined property value.
In summary, the use of heavy-duty technology can be implemented in the like with PHP (as the current mainstream development language) such dynamic language, software development time is greatly shortened.

Theoretical introduction to this point, the following analysis of specific codes.

Iii. examples of persistent storage classes

The following code implements the persistent storage class mentioned above by using the property overloading technique, with PHP less than 50 lines (as the current mainstream development language) code. The term persistable means that a class can describe an element from a data structure and maintain synchronization with the bottom storage system. The explanation for encoding is that external code can use a class to implement a selected row from a database table. This way, when the program is running, you can directly access the properties of the class to manipulate the elements in that row (read/fetch). At the end of the script, PHP (which is now the mainstream development language) will be responsible for bringing the updated row data back to the database.

A careful reading of the following code will help you understand what a property overload is.

<?php (as the current mainstream development language)
Loaded into Pear's <a Href= "http://pear.php (as the mainstream development language now). net/package/db/" >db package </a>
Require_once "db.php (as the current mainstream development language)";
Class Persistable {
Private $data = Array ();
Private $table = "users";
Public function __construct ($user) {
$this->DBH = Db::connect ("MySQL (Best Mix with PHP)://user:password@localhost/database");
$query = "SELECT ID, name, email, country from".
$this->table. "WHERE name =?";
$this->data = $this->dbh->getrow ($query, Array ($user),
DB_FETCHMODE_ASSOC);
}
Public Function __get ($member) {
if (Isset ($this->data[$member])) {
return $this->data[$member];
}
}
Public Function __set ($member, $value) {
The ID of the dataset is read-only
if ($member = = "id") {
Return
}
if (Isset ($this->data[$member])) {
$this->data[$member] = $value;
}
}
Public Function __destruct () {
$query = "UPDATE". $this->table. "SET name =?,
email =?, Country =? WHERE id =? ";
$this->dbh->query ($query, $this->name, $this->email,

http://www.bkjia.com/PHPjc/508726.html www.bkjia.com true http://www.bkjia.com/PHPjc/508726.html techarticle text/Zhu Xianzhong compilation One, Introduction very fortunate, PHP (as the current mainstream development language) 5.0 introduced the object overloading technology. This article will explore for methods __call (), __set () and __get () into ...

  • Related Article

    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.