欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php如何调用方法

程序员文章站 2022-03-09 20:48:50
...
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 'UserFunction1: Param1 : ',$param1,' , Param2 : ',$param2,'<br/>';
	}
	userFunction1('Hello','world');
 
	// 最常见的函数调用 --- 2
	$userFunction2 = function($param1, $param2){
		echo 'UserFunction2: Param1 : ',$param1,' , Param2 : ',$param2,'<br/>';
	};
	$userFunction2('Hello', 'PHP');
 
	// 作为回调函数的函数调用 --- 1
	function funcWithCallback1($callback, $param1, $param2){
		echo 'funcWithCallback1 : ';
		if(is_callable($callback)) $callback($param1, $param2);
	}
	funcWithCallback1($userFunction2,'Hello','world');
 
	// 作为回调函数的函数调用 --- 2 call_user_func
	function funcWithCallback2($callback, $param1, $param2){
		echo 'funcWithCallback2 : ';
		if(is_callable($callback)) call_user_func($callback, $param1, $param2);
	}
	funcWithCallback2($userFunction2,'Hello','world');
 
	// 一个函数调用示例: 以 用户注册,修改密码为例
	$function_pool = array(
		'regist' => array(
			'validateUsername' => function($username){
				return (strtolower($username) !== 'admin') ? true : false;
			},
			'addUser' => function($username, $password){
				$user[$username] = $password;
				echo 'Add user Ok';
				return $user;
			}
		),
		'updatePass' => array(
			'validatePassword' => function($username, $password, $user){
				return (is_array($user) && array_key_exists($username, $user) && $user[$username] == $password);
			},
			'setPassword' => function($username, $password, $user){
				$user[$username] = $password;
				return $user;
			}
		)
	);
 
	function regist($function_pool, $username, $password){
		if(isset($function_pool) && isset($function_pool['regist'])){
			if(isset($function_pool['regist']['validateUsername']) && $function_pool['regist']['validateUsername']($username)){
				return $function_pool['regist']['addUser']($username, $password);
			}else{
				return 'User exists or Uniligel ';
			}
		}
	}
	$user = regist($function_pool, 'Guojunzhou', 'sanyue');
	echo '<pre>';
	print_r($user);
	echo '</pre>';
 
	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 '<pre>';
	print_r($user);
	echo '</pre>';
?>

以上就是php如何调用方法的详细内容,更多请关注其它相关文章!

相关标签: php