Research on object overloading technology in PHP5.0 _ PHP Tutorial

Source: Internet
Author: User
Research on object overloading technology in PHP5.0. Wen Zhu Xianzhong compiled 1. the introduction is very lucky. the object overload technology is introduced in php (as the mainstream development language) 5.0. This article will discuss how to input/compile the _ call () ,__ set () and _ get () methods

I. INTRODUCTION

Fortunately, object overload technology is introduced in php (as the mainstream development language) 5.0. This article will discuss the possibility of overloading method _ call () ,__ set () and _ get. After a brief introduction to the overload theory, we will use two examples to go straight to the topic: the first example to implement the continuous storage class; the second example to find a dynamic getter/setter method.

2. what is object overloading?

When talking about object overloading in php (as the mainstream development language), we need to distinguish the following two types:

· Method overload

· Attribute overloading

In case of Method overloading, we need to define a magic method _ call (), which will implement a general call to the undefined method in the corresponding class. This general method is called only when you want to access undefined methods in the class. Without method overloading, the following example will cause php (as the mainstream development language) to display a fatal error message: Call to undefined method ThisWillFail: bar () in/some/directory/example. php (as the mainstream development language) on line 9 and abortion program execution:

<? Php (as the mainstream development language)
Class ThisWillFail {
Public function foo (){
Return "Hello World! ";
}
}
$ Class = new ThisWillFail;
$ Class-> bar ();
?>

With the help of Method overloading, the code can capture such calls and handle them in a decent manner.

The property overload is similar to the method overload. In this case, the class redirects the read/write operation (also called proxy) to the class attributes, which are not explicitly defined in the class. The special methods here are _ set () and _ get (). Depending on the error report Level. php (as the mainstream development language) usually sends a notification when accessing an undefined attribute, or delays and potentially defines this variable. If you use attribute overloading, the translator can call _ set () when setting an undefined attribute, and call _ get () when accessing an undefined attribute value ().
To sum up, using heavy load technology can greatly shorten the software development time when using dynamic languages such as php (as the current mainstream development language.

The theory is introduced so far, and the specific code is analyzed below.

III. Examples of persistent storage

The following code uses the attribute overload technology to implement the persistent storage class mentioned above with less than 50 lines of php (as the 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 encoding means that external code can use classes to select a row from a database table. In this way, when the program runs, you can directly access the attributes of the class to manipulate the elements (read/retrieve) in the row ). At the end of the script, php (as the mainstream development language) will be responsible for sending the updated row data back to the database.

Carefully studying the code below will help you understand what is property overloading.

<? Php (as the mainstream development language)
// Mount PEAR's <a href = "http://pear.php (as the current mainstream development language). net/package/DB/"> DB package </a>
Require_once "DB. php (as the mainstream development language )";
Class Persistable {
Private $ data = array ();
Private $ table = "users ";
Public function _ construct ($ user ){
$ This-> dbh = DB: Connect ("the best combination of MySQL and 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 ){
// Dataset IDs are 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,

Tip 1. we are lucky to have introduced the object overload technology in php (as the mainstream development language) 5.0. This article will discuss how to add _ call () ,__ set () and _ get ()...

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.