首页  >  文章  >  后端开发  >  php如何调用方法

php如何调用方法

(*-*)浩
(*-*)浩原创
2019-09-29 14:06:265145浏览

php如何调用方法

本类中普通方法调用

$fundList =$this->getOnsaleFundList();

本类中静态方法调用 (推荐学习:PHP视频教程

$fundList =self::getOnsaleFundList();

调用其他类的普通方法

$objFix = new ApiAssemble();

 $objFix->setUser($obj);

调用其他类的静态方法

$objFix = ApiAssemble::setUser($obj);

代码:

<?php
	// 最常见的函数调用 --- 1
	function userFunction1($param1, $param2){
		echo &#39;UserFunction1: Param1 : &#39;,$param1,&#39; , Param2 : &#39;,$param2,&#39;<br/>&#39;;
	}
	userFunction1(&#39;Hello&#39;,&#39;world&#39;);
 
	// 最常见的函数调用 --- 2
	$userFunction2 = function($param1, $param2){
		echo &#39;UserFunction2: Param1 : &#39;,$param1,&#39; , Param2 : &#39;,$param2,&#39;<br/>&#39;;
	};
	$userFunction2(&#39;Hello&#39;, &#39;PHP&#39;);
 
	// 作为回调函数的函数调用 --- 1
	function funcWithCallback1($callback, $param1, $param2){
		echo &#39;funcWithCallback1 : &#39;;
		if(is_callable($callback)) $callback($param1, $param2);
	}
	funcWithCallback1($userFunction2,&#39;Hello&#39;,&#39;world&#39;);
 
	// 作为回调函数的函数调用 --- 2 call_user_func
	function funcWithCallback2($callback, $param1, $param2){
		echo &#39;funcWithCallback2 : &#39;;
		if(is_callable($callback)) call_user_func($callback, $param1, $param2);
	}
	funcWithCallback2($userFunction2,&#39;Hello&#39;,&#39;world&#39;);
 
	// 一个函数调用示例: 以 用户注册,修改密码为例
	$function_pool = array(
		&#39;regist&#39; => array(
			&#39;validateUsername&#39; => function($username){
				return (strtolower($username) !== &#39;admin&#39;) ? true : false;
			},
			&#39;addUser&#39; => function($username, $password){
				$user[$username] = $password;
				echo &#39;Add user Ok&#39;;
				return $user;
			}
		),
		&#39;updatePass&#39; => array(
			&#39;validatePassword&#39; => function($username, $password, $user){
				return (is_array($user) && array_key_exists($username, $user) && $user[$username] == $password);
			},
			&#39;setPassword&#39; => function($username, $password, $user){
				$user[$username] = $password;
				return $user;
			}
		)
	);
 
	function regist($function_pool, $username, $password){
		if(isset($function_pool) && isset($function_pool[&#39;regist&#39;])){
			if(isset($function_pool[&#39;regist&#39;][&#39;validateUsername&#39;]) && $function_pool[&#39;regist&#39;][&#39;validateUsername&#39;]($username)){
				return $function_pool[&#39;regist&#39;][&#39;addUser&#39;]($username, $password);
			}else{
				return &#39;User exists or Uniligel &#39;;
			}
		}
	}
	$user = regist($function_pool, &#39;Guojunzhou&#39;, &#39;sanyue&#39;);
	echo &#39;<pre class="brush:php;toolbar:false">&#39;;
	print_r($user);
	echo &#39;
'; function updatePass($function_pool, $username, $password, $newPass, $user){ if(isset($function_pool) && isset($function_pool['updatePass'])){ if(isset($function_pool['updatePass']['validatePassword']) && $function_pool['updatePass']['validatePassword']($username, $password, $user)){ return $function_pool['updatePass']['setPassword']($username, $newPass, $user); } } } $user = updatePass($function_pool, 'Guojunzhou', 'sanyue', '123456', $user); echo '
&#39;;
	print_r($user);
	echo &#39;
'; ?>

以上是php如何调用方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn