Home > Backend Development > PHP Tutorial > 请教用名称调用函数和传参

请教用名称调用函数和传参

WBOY
Release: 2016-06-23 14:13:52
Original
985 people have browsed it

call_user_func_array 传参

会用call_user_func_array调用已知名称的函数, 但调用已实例化的类就不会了...
	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 ) );
Copy after login

这样会报两个错, 1个说是call_user_func_array调用了非静态方法, 2个是this不能出现在未实例化的类里
其实在call_user_func_array里出现这两个错误都很好理解...
那有其它方法可以实现 利用 函数名调用 已实例化的类的成员函数, 并且传 不定个数的参数么...


回复讨论(解决方案)

	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 ) );
Copy after login


这样看看

我勒个去...居然仅仅是这样...
感谢楼上解答...
怪小弟手册没深看...脑子也不会动...值得检讨

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template