PHP的学习-可变函数
PHP的学习--可变函数
PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。可变函数可以用来实现包括回调函数,函数表在内的一些用途。
可变函数不能用于例如 echo,print,unset(),isset(),empty(),include,require 以及类似的语言结构。需要使用自己的包装函数来将这些结构用作可变函数。
Example #1 可变函数示例
<span style="color: #000000;">php</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> foo() { </span><span style="color: #0000ff;">echo</span> "In foo()<br>\n"<span style="color: #000000;">;}</span><span style="color: #0000ff;">function</span> bar(<span style="color: #800080;">$arg</span> = ''<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "In bar(); argument was '<span style="color: #800080;">$arg</span>'.<br>\n"<span style="color: #000000;">;}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 使用 echo 的包装函数</span><span style="color: #0000ff;">function</span> echoit(<span style="color: #800080;">$string</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$string</span><span style="color: #000000;">;}</span><span style="color: #800080;">$func</span> = 'foo'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls foo()</span><span style="color: #800080;">$func</span> = 'bar'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>('test'); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls bar()</span><span style="color: #800080;">$func</span> = 'echoit'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>('test'); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls echoit()</span>?>
也可以用可变函数的语法来调用一个对象的方法。
<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo{ </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Variable() { </span><span style="color: #800080;">$name</span> = 'Bar'<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>-><span style="color: #800080;">$name</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls the Bar() method</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Bar() { </span><span style="color: #0000ff;">echo</span> "This is Bar"<span style="color: #000000;">; }}</span><span style="color: #800080;">$foo</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Foo();</span><span style="color: #800080;">$funcname</span> = "Variable"<span style="color: #000000;">;</span><span style="color: #800080;">$foo</span>-><span style="color: #800080;">$funcname</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls $foo->Variable()</span>?>
当调用静态方法时,函数调用要比静态属性优先:
Example #3 Variable 方法和静态属性示例
<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo{ </span><span style="color: #0000ff;">static</span> <span style="color: #800080;">$variable</span> = 'static property'<span style="color: #000000;">; </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> Variable() { </span><span style="color: #0000ff;">echo</span> 'Method Variable called'<span style="color: #000000;">; }}</span><span style="color: #0000ff;">echo</span> Foo::<span style="color: #800080;">$variable</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> This prints 'static property'. It does need a $variable in this scope.</span><span style="color: #800080;">$variable</span> = "Variable"<span style="color: #000000;">;Foo</span>::<span style="color: #800080;">$variable</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls $foo->Variable() reading $variable in this scope.</span>?>
可以使用可变函数的方法列表如下:
<span style="color: #000000;">php</span><span style="color: #008080;">array_diff_assoc</span><span style="color: #000000;">()array_diff_key()</span><span style="color: #008080;">array_diff_uassoc</span><span style="color: #000000;">()</span><span style="color: #0000ff;">array</span><span style="color: #000000;">()array_intersect_ukey()</span><span style="color: #008080;">array_map</span><span style="color: #000000;">()</span><span style="color: #008080;">array_merge</span><span style="color: #000000;">()</span><span style="color: #008080;">array_merge_recursive</span><span style="color: #000000;">()</span><span style="color: #008080;">array_multisort</span><span style="color: #000000;">()</span><span style="color: #008080;">array_push</span><span style="color: #000000;">()array_replace()array_replace_recursive()</span><span style="color: #008080;">array_unshift</span><span style="color: #000000;">()</span><span style="color: #008080;">call_user_func</span><span style="color: #000000;">()</span><span style="color: #008080;">call_user_method</span><span style="color: #000000;">()</span><span style="color: #008080;">compact</span><span style="color: #000000;">()dba_open()dba_popen()</span><span style="color: #0000ff;">echo</span><span style="color: #000000;">()forward_static_call()</span><span style="color: #008080;">fprintf</span><span style="color: #000000;">()</span><span style="color: #008080;">fscanf</span><span style="color: #000000;">()httprequestpool_construct()ibase_execute()ibase_set_event_handler()ibase_wait_event()</span><span style="color: #0000ff;">isset</span><span style="color: #000000;">()</span><span style="color: #0000ff;">list</span><span style="color: #000000;">()maxdb_stmt_bind_param()maxdb_stmt_bind_result()mb_convert_variables()newt_checkbox_tree_add_item()newt_grid_h_close_stacked()newt_grid_h_stacked()newt_grid_v_close_stacked()newt_grid_v_stacked()newt_win_choice()newt_win_entries()newt_win_menu()newt_win_message()newt_win_ternary()</span><span style="color: #008080;">pack</span><span style="color: #000000;">()</span><span style="color: #008080;">printf</span><span style="color: #000000;">()</span><span style="color: #008080;">register_shutdown_function</span><span style="color: #000000;">()</span><span style="color: #008080;">register_tick_function</span><span style="color: #000000;">()</span><span style="color: #008080;">session_register</span><span style="color: #000000;">()</span><span style="color: #008080;">setlocale</span><span style="color: #000000;">()</span><span style="color: #008080;">sprintf</span><span style="color: #000000;">()</span><span style="color: #008080;">sscanf</span><span style="color: #000000;">()</span><span style="color: #0000ff;">unset</span><span style="color: #000000;">()</span><span style="color: #008080;">var_dump</span><span style="color: #000000;">()w32api_deftype()w32api_init_dtype()w32api_invoke_function()</span><span style="color: #008080;">wddx_add_vars</span><span style="color: #000000;">()</span><span style="color: #008080;">wddx_serialize_vars</span>()
摘自:http://php.net/manual/zh/functions.variable-functions.php

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

For PHP developers, encountering the "Undefinedvariable" warning message is a very common thing. This situation usually occurs when trying to use undefined variables, which will cause the PHP script to fail to execute normally. This article will introduce how to solve the problem of "PHPNotice:Undefinedvariable: in solution". Cause of the problem: In a PHP script, if a variable is not initialized or assigned a value, "Un

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is
