PHP 5.0 中的对象重载技术研究
文/朱先忠编译
一、简介
很幸运,php(做为现在的主流开发语言) 5.0中引入了对象重载技术。本文将探讨对于方法__call(),__set()以及__get()进行重载的可能性。在对重载理论作简单介绍后,我们将通过两个例子直奔主题:第一例,实现持续存储类;第二例,找到一种实现动态的getter/setter的方法。
二、什么是对象重载?
在php(做为现在的主流开发语言)中谈到对象重载时,我们要区别两种类型:
·方法重载
·属性重载
在方法重载的情况下,我们要定义一个魔术般的方法__call(),它将实现一个在相应类中对未定义方法的笼统调用。只有当你想存取类中未定义的方法时,这种笼统方法才会被调用。在没有方法重载的情况下,下面的例子将导致php(做为现在的主流开发语言)显示一条致命错误信息:Call to undefined method ThisWillFail::bar() in/some/directory/example.php(做为现在的主流开发语言) on line 9并流产程序的执行:
<?php(做为现在的主流开发语言)
class ThisWillFail {
public function foo() {
return "Hello World!";
}
}
$class = new ThisWillFail;
$class->bar();
?>
借助方法重载的帮助,代码能够捕获到这种调用且能够体面地给以处理。
属性重载与方法重载差不多。这种情况下,类把读/写操作重定向(亦可称代理)到类的属性,这些属性在类中没有显式定义。这里的专门方法是__set()和__get()。依赖于错误报告等级,php(做为现在的主流开发语言)翻译器通常在存取一个未定义的属性时,或者发出一个通知,或者推迟一下并潜在地定义这个变量。而如果使用属性重载,翻译器却可以在设置一个未定义的属性时调用__set(),而在存取一个未定义的属性值时调用__get()。
综上所述,利用重载技术可以实现在象用php(做为现在的主流开发语言)这样的动态语言进行时软件开发时间的大大缩短。
理论介绍至此,下面分析具体编码。
三、持续性存储类举例
下列代码,通过使用属性重载技术,用少于50行的php(做为现在的主流开发语言)代码实现了上面所提到的持续性存储类。术语persistable意味着类可以从一个数据结构中描述一个元素,并保持与底端存储系统的同步。用编码的解释就是,外部代码可以使用类来实现从一个数据库表中选定一行。这样,在程序运行时,可以直接存取类的属性来操纵该行中的元素(读/取)。在脚本结束时,php(做为现在的主流开发语言)将负责把更新的行数据回送到数据库中去。
精心研读下面代码将有助于你理解什么是属性重载。
<?php(做为现在的主流开发语言)
//装入PEAR的 <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(和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的ID是只读的
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,

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use