PHP Reflection class carries parameters to execute the reflection object method to pass in parameter function example parsing.

Source: Internet
Author: User
PHP Reflection class carries parameters to execute the reflection object method to pass in parameter function example parsing. Background: for the before after and controller and action functions in the early kohana framework, you can use the reflection class to flexibly execute the functions in the specified sequence. if an exception occurs during execution, you can capture the output through the exception capture provided by PHP, which involves the problem of passing the parameters of the class object. how can you transmit multiple parameters or even reference parameters to the functions in the reflection class, this PHP also provides the ReflectionMethod: invokeArgs passed by the function in the corresponding reflection class.

The code snippet is as follows:

$ Class = new ReflectionClass ('person '); // creates a reflection class for the Person class $ instance = $ class-> newInstanceArgs ($ args ); // It is equivalent to instantiating the Person class public stdclass newInstance (mixed * args) // test whether the input object is an instance of this class

Refer to more reflection articles:

Http://flandycheng.blog.51cto.com/855176/326021/

Http://www.phperz.com/article/14/0809/17371.html

Research by myself: http://justwinit.cn/post/2090/

ReflectionMethod: invokeArgs

(PHP 5> = 5.1.0)

ReflectionMethod: invokeArgs-execution with parameters

Description

Public mixed ReflectionMethod: invokeArgs (object $ object, array $ args)

Use arrays to send parameters to methods and execute them.

Parameters

Object

The object that calls the method. if it is a static object, set it to null.

Args

The method parameters transmitted using array.

Return value

Return method return value

Error/exception

If the instance specified by the object cannot execute the method, a ReflectionException is generated.

If the method call fails, a ReflectionException is generated.

Example

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 '));

?>

The above routine will output:

Hello Mike

Note

Note:

If a function has parameters that need to be referenced, they must be passed in as references.

See

ReflectionMethod: invoke ()-Invoke

_ Invoke ()

Call_user_func_array ()-Call the callback function and use an array parameter as the callback function parameter.

From: http://help.bitscn.com/php/reflectionmethod.invokeargs.html

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.