How to call method in php

(*-*)浩
Release: 2023-02-25 15:46:02
Original
5160 people have browsed it

How to call method in php

Common method calls in this class

$fundList =$this->getOnsaleFundList();

Static method call in this class (Recommended learning: PHP video tutorial)

$fundList =self::getOnsaleFundList();

Call ordinary methods of other classes

$objFix = new ApiAssemble();

$objFix->setUser($obj);

Call static methods of other classes

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

Code:

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

The above is the detailed content of How to call method in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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