首頁  >  文章  >  後端開發  >  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