Could you call a function and pass parameters by name?

Source: Internet
Author: User
If you want to call a function by name or pass the parameter, call_user_func_array will be used to call a function with a known name. & nbsp; but it will not be used to call the instantiated class... class & nbsp; class_a {public & nbsp; $ var_a & nbsp; 0; public & nbsp; function & nbsp; fun_1 & nbsp; (& nbsp; $ var & nb: call functions and pass parameters by name
Call_user_func_array will be used to call a function with a known name, but the instantiated class will not be called...

class class_a
{
public $var_a= 0;

public function fun_1 ( $var )
{
var_dump( $this->var_a + $var );
}
};

$obj_a= new class_a();
$obj_a->var_a= 6;

call_user_func_array( array( 'class_a', 'fun_1' ), array( 5 ) );

Two errors will be reported. one is call_user_func_array, which calls non-static methods. The other two are not allowed to appear in the uninstantiated class.
In fact, the two errors in call_user_func_array are well understood...
Is there any other way to call the member functions of the instantiated class by using the function name, and pass parameters of an indefinite number...

Call_user_func_array

Share:


------ Solution --------------------


class class_a
{
public $var_a= 0;

public function fun_1 ( $var )
{
var_dump( $this->var_a + $var );
}
};

$obj_a= new class_a();
$obj_a->var_a= 6;

call_user_func_array( array($obj_a, 'fun_1' ), array( 5 ) );



Let's take a look.

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.