Research on object overload technology in PHP 5.0

Source: Internet
Author: User
Tags array constructor empty execution pear php code php programming php script
   First, Introduction

Luckily, object overload technology was introduced in PHP 5.0. This article explores the possibility of overloading the method __call (), __set (), and __get (). After a brief introduction to the overload theory, we will get to the topic through two examples: The first example is to implement the continuous storage class; The second example is to find a way to implement dynamic Getter/setter.

   second, what is Object overload?

When it comes to object overloading in PHP, we distinguish between two types:

• Method overload

• Attribute overloading

In the case of method overloading, we want to define a magic method __call (), which implements a generic invocation of undefined methods in the corresponding class. This generic method is invoked only if you want to access a method that is not defined in the class. In the absence of method overloading, the following example causes PHP to display a fatal error message: Call to undefined methods Thiswillfail::bar () in/some/directory/example.php on line 9 and the execution of the abortion procedure:

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


With the help of method overloading, code can capture such calls and gracefully handle them.

Property overloads are about the same as method overloads. In this case, the class redirects read/write operations (also known as proxies) to the properties of the class, which are not explicitly defined in the class. The special method here is __set () and __get (). Depending on the error reporting level, the PHP translator usually accesses an undefined attribute, either sends a notification, or delays and potentially defines the variable. If the property overload is used, the translator can call __set () when an undefined property is set, and call __get () when accessing an undefined property value.
To sum up, the use of overload technology can be used in a dynamic language such as PHP, software development time is greatly shortened.

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

   iii. Examples of persistent storage classes

The following code implements the persistent storage class mentioned above by using the attribute overload technique with less than 50 lines of PHP 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 explanation is that the external code can use a class to implement the selection of a row from a database table. In this way, when the program is running, you can directly access the properties of the class to manipulate the elements (read/fetch) in that row. At the end of the script, PHP is responsible for echoing the updated row data back into the database.

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

<?php
Mount Pear's <a Href= "http://pear.php.net/package/DB/" >db package </a>
Require_once "db.php";
Class Persistable {
Private $data = Array ();
Private $table = "users";
Public function __construct ($user) {
$this->DBH = Db::connect ("Mysql://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,
$this->country, $this->id);
}
}
$class = new Persistable ("Martin Jansen");
$class->name = "John Doe";
$class->country = "United States";
$class->email = "john@example.com";
? >


The first problem you encounter may be __construct (), which is the new constructor method introduced in PHP 5. In the PHP 4 era, constructors always match their class names. This is no longer the case in PHP 5. You do not need to know too much about constructor methods, except to invoke it to create an instance of a class, and note that a parameter is used-executes a database based on this parameter. This constructor assigns the result of the query to the class attribute $data.

Next, the program defines two special methods __get () and __set (). You should be familiar with them: __get () is used to read undefined property values, and __set () is used to modify undefined property values.

This means that whenever an undefined attribute is read/written from a persistent storage class, these specialized methods are responsible for managing the information in the property array variable $data instead of directly changing the properties of the Class (remember: The variable $data contains a row from the database!).).

The last method in the class is the opposite of __construct ()-The destructor __destruct (). PHP invokes the destructor in the script shutdown phase, typically when PHP script execution is almost over. The destructor writes the information from the $data attribute back to the database. This is exactly what the previous sync (synchronization) terminology means.

As you may have noticed, the code here uses the Database abstraction layer package of pear (DB Abstraction layer package). In fact, this does not matter, through other ways and database communication can also explain the theme of this article.

If you look carefully, you will find that the description of the persistent storage class is relatively simple. The example involves only one database table, not more complex data models, such as the use of left join and other complex database manipulation techniques. However, you do not have to be bound by this constraint, and with the help of attribute overloading, you can use your own ideal database model. With just a little code to add, you can use complex database features in that persistent storage class.

There is also a small problem-no error-handling mechanism is introduced when the query fails in the destructor. It is the nature of the destructor that makes it impossible to display the appropriate error message in this case because building HTML flags is often over before PHP invokes the destructor.

To solve this problem, you can rename the __destruct () to a name like SaveData () and manually execute the method at some point in the calling script. This does not change the concept of a class's persistent storage; it's just a few more lines of code. As a choice, you can also use function error_log () in the destructor to record error messages in the system-wide error logging file.

This is how the property overload works. Let's discuss the method overload below.

  Examples of method overloading

1. The dynamic Getter/setter method

The following code implements the dynamic Getter/setter method to control the class with the help of the method overload. Here we combine the source code for analysis:

Class Dynamicgettersetter {
Private $name = "Martin Jansen";
Private $starbucksdrink = "caramel Cappuccino swirl";
function __call ($method, $arguments) {
$prefix = Strtolower (substr ($method, 0, 3));
$property = Strtolower (substr ($method, 3));
if (Empty ($prefix) | | empty ($property)) {
Return
}
if ($prefix = = "Get" && isset ($this-> $property)) {
return $this-> $property;
}
if ($prefix = = "Set") {
$this-> $property = $arguments [0];
}
}
}
$class = new Dynamicgettersetter;
echo "Name:". $class->getname (). "\ n";
echo "Favourite Starbucks flavour:". $class->getstarbucksdrink (). "\ n";
$class->setname ("John Doe");
$class->setstarbucksdrink ("Classic Coffee");
echo "Name:". $class->getname (). "\ n";
echo "Favourite Starbucks flavour:". $class->getstarbucksdrink (). "\ n";
? >


Obviously, the two attributes $name and $starbucksdrink are private, which means that they cannot be accessed from the outside of the class. In object-oriented programming, it is very common to implement public getter/setter methods to access or modify the values of non-public properties. These are tedious things to achieve, and they are time-consuming and energy-intensive.

This problem can be solved easily by means of overload. Instead of implementing the Getter/setter method for each attribute, a generic __call () method is implemented. This means that when an undefined getter/setter method such as SetName () or Getstarbucksdrink () is invoked, PHP does not produce a fatal error and is aborted, but instead executes (or proxies to) the Magic __call () method.

This is a brief introduction, below we to __call () for an in-depth analysis.

2. Detailed analysis of the __call () method

The first parameter of __call () is the original and not yet determined method (such as SetName), and the second parameter is a one-dimensional array of numeric indices that contains all the parameters of the original method. Calling an undefined method with two arguments ("Martin" and "42") produces the following array:

$class->thismethoddoesnotexist ("Martin", 42);
/guide The second parameter of __call ()
Array
(
[0] => Martin
[1] => 42
)


Within Method __call (), if the original method starts with a get or set, a calculation is performed to determine whether the code calls a Getter/setter method. Also, this method further analyzes the other component of the method name (minus the three characters that started), because the string that follows is the name of the property that Getter/setter references.

If a getter/setter is indicated in the method name, the method either returns the corresponding property value or sets the value of the first parameter of the original method. If not, it does nothing and continues to execute the program as if nothing had happened.

3. Achieving the Goals

Essentially, there is a way to allow code to dynamically invoke arbitrary getter/setter methods, as appropriate for any property, and this algorithm exists. This is handy when developing a program prototype in the short term: instead of spending a lot of time implementing getters/setters, developers can focus on modeling APIs and ensure that the application is fundamentally correct. Incorporating the __call () approach into an abstract class may even make your code reusable in future PHP engineering development!

4. Beyond the shortage

There are disadvantages when there are advantages. There are several disadvantages to this approach: larger projects can use tools such as Phpdocumentor to track the API structure. With the dynamic approach described above, all getter/setter methods certainly do not appear in automatically generated documents, which is not necessarily explained.

Another disadvantage is that the code outside the class can access each private property within the class. When using the true Getter/setter method, it is possible to distinguish between private properties that can be accessed by external code and "real" private properties that are not visible outside of the class-because we have method overloads, and there are virtual getter and setter methods available.

   v. Conclusion

This paper analyzes two cases of object overload in PHP 5.0 through two examples. I hope this article can help you improve the efficiency of PHP programming! At the same time, you should be aware of the shortcomings of this approach.



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.