PHP クラス関数または関数を動的に作成する

巴扎黑
リリース: 2016-11-29 11:07:25
オリジナル
1598 人が閲覧しました

1. 基本的な

変数関数:

<?php  
$func = &#39;test&#39;;  
  
function test(){  
    echo &#39;yes !&#39;;  
}  
  
$func();  
?>
ログイン後にコピー

ランダム関数:

<?php  
$newfunc = create_function(&#39;$a,$b&#39;, &#39;return $a.$b;&#39;);  
echo "New anonymous function: $newfunc<br>";  
echo $newfunc(&#39;just&#39;, &#39;coding&#39;);  
?>
ログイン後にコピー

create_function — 匿名 (ラムダスタイル) 関数を作成します

匿名関数を作成します。この関数は主にunsortとarray_walkのコールバック関数で使用されます

$a,$bはパラメータ、'return $a,$b'は関数のコードです

​​

コールバック関数:

<?php     
//5.3 以前     
$array = array( &#39;asbc&#39;, &#39;ddd&#39;, &#39;tttt&#39;, &#39;qqq&#39;);     
array_walk($array,create_function(&#39;&$item&#39;,&#39;$item=strtoupper($item);&#39;) ); //function(&$itm){$itm = strtoupper($itm);}     
print_r($array);  
  
//5.3 以后     
$array = array( &#39;asbc&#39;, &#39;ddd&#39;, &#39;tttt&#39;, &#39;qqq&#39;);     
array_walk($array,function(&$itm){$itm = strtoupper($itm);});      
print_r($array);  
?>
ログイン後にコピー

array_walk(array 、関数、ユーザーデータ ...)

array_walk() 関数は、配列内の各要素にコールバック関数を適用します。成功した場合は TRUE を返し、そうでない場合は FALSE を返します。

通常、関数は 2 つのパラメータを受け取ります。配列パラメータの値が最初のパラメータとして使用され、キー名が 2 番目のパラメータとして使用されます。オプションのパラメーター userdata が指定されている場合、それは 3 番目のパラメーターとしてコールバック関数に渡されます。

2. クラス関数のインスタンスの動的作成

<?php  
/* create class */  
class Record {  
    
    /* record information will be held in here */  
    private $info;  
    
    /* constructor */  
    function Record($record_array) {  
        $record_array[&#39;body&#39;] = &#39;this is a new attribution&#39;;  
        $this->info = $record_array;  
    }  
    
    /* dynamic function server */  
    function __call($method,$arguments) {  
        $meth = $this->from_case(substr($method,3,strlen($method)-3));  
        return array_key_exists($meth,$this->info) ? $this->info[$meth] : false;  
    }  
    
    function from_case($str) {  
        $str[0] = strtolower($str[0]);  
        $func = create_function(&#39;$c&#39;, &#39;return "_" . strtolower($c[1]);&#39;); // function ($c) { return "_" . strtolower($c[1]); }  
        return preg_replace_callback(&#39;/([A-Z])/&#39;, $func, $str);  
    }    
}  
  
  
/* usage */  
$Record = new Record(  
    array(  
        &#39;id&#39; => 12,  
        &#39;title&#39; => &#39;Greatest Hits&#39;,  
        &#39;description&#39; => &#39;The greatest hits from the best band in the world!&#39;  
    )  
);  
  
/* proof it works! */  
echo &#39;The ID is:  &#39;.$Record->getId().&#39;<br>&#39;; // returns 12    
echo &#39;The Title is:  &#39;.$Record->getTitle().&#39;<br>&#39;; // returns "Greatest Hits"  
echo &#39;The Description is:  &#39;.$Record->getDescription().&#39;<br>&#39;; //returns "The greatest hits from the best band in the world!"  
echo &#39;The Body is:  &#39;.$Record->getBody(); //returns "The greatest hits from the best band in the world!"  
?>
ログイン後にコピー

重要なポイントは: __call と create_function


関連ラベル:
php
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!