Home >Backend Development >PHP Tutorial >Research on Object Overloading Technology in PHP 5.0_PHP Tutorial
Text/Compiled by Zhu Xianzhong
1. Introduction
Fortunately, php (as the current mainstream development language) introduced object overloading technology in 5.0. This article will explore the possibility of overloading the methods __call(), __set() and __get(). After a brief introduction to overloading theory, we will go straight to the topic through two examples: the first example is to implement a persistent storage class; the second example is to find a way to implement dynamic getter/setter.
2. What is object overloading?
When talking about object overloading in php (as the current mainstream development language) , we have to distinguish two types:
· Method overloading
·Attribute overloading
In the case of method overloading, we have to define a magic method __call(), which will implement a general call to undefined methods in the corresponding class . This general method is called only when you want to access an undefined method in the class. In the absence of method overloading, the following example will cause php(as the current mainstream development language) to display a fatal error message: Call to undefined method ThisWillFail::bar() in/some/ directory/example.php(as the current mainstream development language) on line 9 and abort the execution of the program:
<?php(as the current 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 catch this call and handle it gracefully.
Attribute overloading is similar to method overloading. In this case, the class redirects (also called proxying) read/write operations to properties of the class that are not explicitly defined in the class. The specialized methods here are __set() and __get(). Depending on the level of error reporting, the PHP translator will usually either issue a notification when accessing an undefined property, or defer and potentially define the 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, the use of overloading technology can greatly shorten the software development time when using dynamic languages such as php (as the current mainstream development language)
. This is the theoretical introduction, let’s analyze the specific coding below.
3. Examples of persistent storage classes
The following code uses less than 50 lines of php
(as the current mainstream development language) by using attribute overloading technologyThe code implements the persistent storage class mentioned above. The term persistent means that the class can describe an element from a data structure and remain synchronized with the underlying storage system. In coding terms, external code can use classes to select a row from a database table. In this way, when the program is running, you can directly access the attributes of the class to manipulate the elements in the row (read/fetch). At the end of the script, php (as the current mainstream development language) will be responsible for sending the updated row data back to the database. Carefully studying the following code will help you understand what attribute overloading is.
<?php(as the current mainstream development language)
// Load 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 current mainstream development language)";
class Persistable {
private $data = array();
private $table = "users";
public function __construct($user) {
$this->dbh = DB::Connect( "MySQL(The best combination 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,