search
Homephp教程php手册php中如何调用用户自定义的函数(实例详解)

以下是对php中调用用户自定义函数的方法进行了详细的分析介绍,需要的朋友可以过来参考下

先放上来别人的例子吧:

call_user_func函数类似于一种特别的调用函数的方法,使用方法如下:   

代码如下:

function a($b,$c)    
{    
echo $b;    
echo $c;    
}    
call_user_func('a', "111","222");    
call_user_func('a', "333","444");    
//显示 111 222 333 444    
?>

调用类内部的方法比较奇怪,居然用的是array,不知道开发者是如何考虑的,当然省去了new,也是满有新意的:   

代码如下:

class a {    
function b($c)    
{    
echo $c;    
}    
}    
call_user_func(array("a", "b"),"111");    
//显示 111    
?>

call_user_func_array函数和call_user_func很相似,只不过是换了一种方式传递了参数,让参数的结构更清晰:   

代码如下:

function a($b, $c)    
{    
echo $b;    
echo $c;    
}    
call_user_func_array('a', array("111", "222"));    
//显示 111 222    
?>

call_user_func_array函数也可以调用类内部的方法的   

代码如下:

Class ClassA    
{    
function bc($b, $c) {    
     $bc = $b + $c;    
echo $bc;    
}    
}    
call_user_func_array(array('ClassA','bc'), array("111", "222"));    
//显示 333    
?>

call_user_func函数和call_user_func_array函数都支持引用,这让他们和普通的函数调用更趋于功能一致:   

代码如下:

function a($b)    
{    
$b++;    
}    
$c = 0;    
call_user_func('a', $c);    
echo $c;//显示 1    
call_user_func_array('a', array($c));    
echo $c;//显示 2

另:call_user_func函数和call_user_func_array函数都支持引用。

代码如下:

<?php
function increment(&$var)
{
    $var++;
}
$a = 0;
call_user_func(&#39;increment&#39;, $a);
echo $a; // 0
call_user_func_array(&#39;increment&#39;, array(&$a)); // You can use this instead
echo $a; // 1
?>

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment