search
Homephp教程php手册用PHP的反射实现委托模式

委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧,许多其他的模式,如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。

动态委托的介绍:动态委托概念来自于Jakarta 字节码工程库 (Byte-Code Engineering Library, BCEL)。它能够分析存在的类,并且对于接口,抽象类,甚至运行时的具体类来说,它能够生成以字节编码委托类。

被委托的接口/类应该满足如下条件:动态委托最多只能委托一个类,但是能够代理多个接口。这个限制来自于Java的单继承模式。一个Java类最多只有一个父类。既然生成的委托类把被委托类作为它的父类,那么指定多个被委托类是不合理的。如果没有指定被委托类,那么缺省的父类就是Object。

下面是PHP 反射机制实现动态代理的代码:

<?php
class Fruit 
{
	function callFruit() 
	{
		print "Generate an Apple";
	}
}
class FruitDelegator 
{
	private $targets;
	
	function __construct() 
	{
		$this->target[] = new Fruit();
	}
	
	function __call($name, $args) 
	{
		foreach ($this->target as $obj) 
		{
			$r = new ReflectionClass($obj);
			if ($method = $r->getMethod($name)) 
			{
				if ($method->isPublic() && !$method->isAbstract()) 
				{
					return $method->invoke($obj, $args);
				}
			}
		}
	}
}
$obj = new FruitDelegator();
$obj->callFruit();
// 运行结果
// Generate an Apple
?>

可见,通过代理类FruitDelegator来代替Fruit类来实现他的方法。

同样的,如下的代码也是能够运行的:

<?php
class Color 
{
	function callColor() 
	{
		print "Generate Red";
	}
}
class ColorDelegator 
{
	private $targets;
	
	function addObject($obj) 
	{
		$this->target[] = $obj;
	}
	
	function __call($name, $args) 
	{
		foreach ($this->target as $obj) 
		{
			$r = new ReflectionClass($obj);
			if ($method = $r->getMethod($name)) 
			{
				if ($method->isPublic() && !$method->isAbstract()) 
				{
					return $method->invoke($obj, $args);
				}
			}
		}
	}
}
$obj = new ColorDelegator();
$obj->addObject(new Color());
$obj->callColor();
?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use