PHP反射类带参数执行反射对象方法传入参数函数示例解析。
背景:对于kohana早期框架对类里面的before after以及controller和action的函数均可以用反射类来灵活按既定顺序执行,而在执行的时候假如异常,可以通过PHP提供的异常捕获进行捕获输出,这就涉及到类变对象后其参数的传递问题了,怎么给反射类里面的函数传递多参数乃至引用参数,这个PHP也提供了对应的反射类里函数传入的方法ReflectionMethod::invokeArgs。
使用代码片段如下:
$class = new ReflectionClass('Person'); // 建立 Person这个类的反射类 $instance = $class->newInstanceArgs($args); // 相当于实例化Person 类 public stdclass newInstance(mixed* args)//测试传入的对象是否为该类的一个实例
参考更多相关反射的文章:
http://flandycheng.blog.51cto.com/855176/326021/
http://www.phperz.com/article/14/0809/17371.html
自己研究:http://justwinit.cn/post/2090/
ReflectionMethod::invokeArgs
(PHP 5 >= 5.1.0)
ReflectionMethod::invokeArgs — 带参数执行
说明
public mixed ReflectionMethod::invokeArgs ( object $object , array $args )
使用数组给方法传送参数,并执行他。
参数
object
调用方法的对象,如果是静态对象,设置为 null
args
使用 array 传送的方法参数。
返回值
返回方法返回值
错误/异常
如果 object 指定的实例无法执行方法,那么产生 ReflectionException 异常。
如果方法调用失败,产生 ReflectionException
范例
Example #1 ReflectionMethod::invokeArgs() example
class HelloWorld {
public function sayHelloTo($name) {
return 'Hello ' . $name;
}
}
$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invokeArgs(new HelloWorld(), array('Mike'));
?>
以上例程会输出:
Hello Mike
注释
Note:
如果函数有参数需为引用,那么它们必须以引用方式传入。
参见
ReflectionMethod::invoke() - Invoke
__invoke()
call_user_func_array() - 调用回调函数,并把一个数组参数作为回调函数的参数
来自:http://help.bitscn.com/php/reflectionmethod.invokeargs.html

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
