>  기사  >  백엔드 개발  >  PHP 클래스 함수 또는 함수를 동적으로 생성

PHP 클래스 함수 또는 함수를 동적으로 생성

巴扎黑
巴扎黑원래의
2016-11-29 11:07:251590검색

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 — 익명(lambda- style) 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,function,userdata...)

array_walk() 함수는 배열의 각 요소에 콜백 함수를 적용합니다. 성공하면 TRUE를 반환하고 그렇지 않으면 FALSE를 반환합니다.

일반적으로 함수는 두 개의 매개변수를 받습니다. 배열 매개변수의 값이 첫 번째로 사용되고, 키 이름이 두 번째로 사용됩니다. 선택적 매개변수 userdata가 제공되면 콜백 함수에 세 번째 매개변수로 전달됩니다.

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입니다


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.