php _call()与call_user_func_array()了解
php __call()与call_user_func_array()理解
1. mixed __call ( string name, array arguments )
The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method.
译文: 这个魔术方法允许用户调用类中不存在的方法,它用于实现那些 依赖于在被调用时的真正方法名的方法. 典型的例子是用来实现代理. 方法的参数$arguments是一个数组 ,__call()的返回值返回给方法调用者
白话文: 这个方法主要是用来实现动态方法调用, 如果再一个类定义了__call()这个方法, 当用户调用这个类的一个不存在的方法时,他可以使用调用的那个不存在的方法的方法名和参数做出用户定义在__call()方法体内的相应操作,此时__call()方法的参数就是被调用的那个不存在的方法的方法名和参数
例子
<?php class Person { function talk( $sound ) { echo $sound; } function __call( $method , $args ) { echo 'you call method ' . $method . '<br>'; echo 'and the arguments are <br>'; var_dump( $args ); } } $person = new Person(); $person->test( 1 , TRUE ); ?>
程序输出
and the arguments are
array
0 => int 1
1 => boolean true
2. mixed call_user_func_array ( callback function, array param_arr )
Call a user defined function with the parameters in param_arr.
参数
function
The function to be called.
param_arr
The parameters to be passed to the function, as an indexed array.
返回值
Returns the function result, or FALSE on error.
此方法可以通过传入类名,类中得方法名和方法参数达到动态调用方法的效果
例子
<?php class Person { function talk( $sound ) { echo $sound; } function __call( $method , $args ) { echo 'you call method ' . $method . '<br>'; echo 'and the arguments are <br>'; var_dump( $args ); } } $person = new Person(); call_user_func_array( array( $person , 'talk' ) , array( 'hello' ) ); ?>
程序输出
两个方法共用,实现代理模型
class Person { function talk( $sound ) { echo $sound; } function __call( $method , $args ) { echo 'you call method ' . $method . '<br>'; echo 'and the arguments are <br>'; var_dump( $args ); } } class PersonProxy { private $person; function __construct() { $this->person = new Person(); } function __call( $method , $args ) { call_user_func_array( array( $this->person , $method ) , $args ); } } $person_proxy = new PersonProxy(); $person_proxy->talk( 'thank you' );
程序输出


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

function是函數的意思,是一段具有特定功能的可重複使用的程式碼區塊,是程式的基本組成單元之一,可以接受輸入參數,執行特定的操作,並傳回結果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護性。

使用PHP進行web應用開發時,很多時候會需要使用資料庫。而在使用資料庫時,錯誤提示是非常常見的事情。其中,PHPFatalerror:Calltoamemberfunctionfetch()是比較常見的錯誤,它會在使用PDO查詢資料庫時出現。那麼,這個錯誤是怎麼造成的,以及該如何解決呢?本文將為大家詳細闡述。一、錯誤產生原

標題:C#中使用Array.Sort函數對陣列進行排序的範例正文:在C#中,陣列是一種常用的資料結構,經常需要對陣列進行排序運算。 C#提供了Array類,其中有Sort方法可以方便地對陣列進行排序。本文將示範如何使用C#中的Array.Sort函數對陣列進行排序,並提供具體的程式碼範例。首先,我們要先了解Array.Sort函數的基本用法。 Array.So

在本文中,我們將了解enumerate()函數以及Python中「enumerate()」函數的用途。什麼是enumerate()函數? Python的enumerate()函數接受資料集合作為參數並傳回一個枚舉物件。枚舉物件以鍵值對的形式傳回。 key是每個item對應的索引,value是items。語法enumerate(iterable,start)參數iterable-傳入的資料集合可以作為枚舉物件傳回,稱為iterablestart-顧名思義,枚舉物件的起始索引由start定義。如果我們忽

在PHP中,有許多強大的陣列函數可以讓陣列的操作更加方便和快速。當我們需要將兩個陣列拼成一個關聯數組時,可以使用PHP的array_combine函數來實現這一操作。這個函數其實是用來將一個陣列的鍵當作另一個陣列的值,合併成一個新的關聯數組。接下來,我們將會講解如何使用PHP中的array_combine函數將兩個陣列拼成關聯數組。了解array_comb

在進行PHP編程時,我們常常需要將數組合併。 PHP提供了array_merge()函數來完成數組合併的工作,但是當數組中存在相同的鍵時,函數會覆寫原來的值。為了解決這個問題,PHP在語言中還提供了一個array_merge_recursive()函數,該函數可以合併數組並保留相同鍵的值,使得程式的設計變得更加靈活。 array_merge

MySQL.proc表的功能與功能詳解MySQL是一種流行的關係型資料庫管理系統,開發者在使用MySQL時常常會涉及到預存程序(StoredProcedure)的建立與管理。而MySQL.proc表則是一個非常重要的系統表,它儲存了資料庫中所有的預存程序的相關信息,包括預存程序的名稱、定義、參數等。在本文中,我們將詳細解釋MySQL.proc表的作用與功能

clearstatcache()函數用於清除檔案狀態快取。 PHP快取以下函數傳回的資訊−stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms()這樣做是為了提供更好的性能。語法voidclearstatecache()參數NA傳回值clearstatcache(
