动态创建php 类函数或函数

巴扎黑
发布: 2016-11-29 11:07:25
原创
1985人浏览过

1. 基础

变量函数:  

<?php  
$func = 'test';  
  
function test(){  
    echo 'yes !';  
}  
  
$func();  
?>
登录后复制

随机函数:

<?php  
$newfunc = create_function('$a,$b', 'return $a.$b;');  
echo "New anonymous function: $newfunc<br>";  
echo $newfunc('just', 'coding');  
?>
登录后复制

create_function — Create an anonymous (lambda-style) function

创建一个匿名函数。这个函数主要作用在unsort和array_walk的回调函数

立即学习PHP免费学习笔记(深入)”;

$a,$b为参数,'return $a,$b' 为函数的代码

回调函数 :

怪兽AI数字人
怪兽AI数字人

数字人短视频创作,数字人直播,实时驱动数字人

怪兽AI数字人 44
查看详情 怪兽AI数字人
<?php     
//5.3 以前     
$array = array( 'asbc', 'ddd', 'tttt', 'qqq');     
array_walk($array,create_function('&$item','$item=strtoupper($item);') ); //function(&$itm){$itm = strtoupper($itm);}     
print_r($array);  
  
//5.3 以后     
$array = array( 'asbc', 'ddd', 'tttt', 'qqq');     
array_walk($array,function(&$itm){$itm = strtoupper($itm);});      
print_r($array);  
?>
登录后复制

array_walk(array,function,userdata...)

array_walk() 函数对数组中的每个元素应用回调函数。如果成功则返回 TRUE,否则返回 FALSE。

典型情况下 function 接受两个参数。array 参数的值作为第一个,键名作为第二个。如果提供了可选参数 userdata ,将被作为第三个参数传递给回调函数。 

2. 实例动态创建类函数

<?php  
/* create class */  
class Record {  
    
    /* record information will be held in here */  
    private $info;  
    
    /* constructor */  
    function Record($record_array) {  
        $record_array['body'] = 'this is a new attribution';  
        $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('$c', 'return "_" . strtolower($c[1]);'); // function ($c) { return "_" . strtolower($c[1]); }  
        return preg_replace_callback('/([A-Z])/', $func, $str);  
    }    
}  
  
  
/* usage */  
$Record = new Record(  
    array(  
        'id' => 12,  
        'title' => 'Greatest Hits',  
        'description' => 'The greatest hits from the best band in the world!'  
    )  
);  
  
/* proof it works! */  
echo 'The ID is:  '.$Record->getId().'<br>'; // returns 12    
echo 'The Title is:  '.$Record->getTitle().'<br>'; // returns "Greatest Hits"  
echo 'The Description is:  '.$Record->getDescription().'<br>'; //returns "The greatest hits from the best band in the world!"  
echo 'The Body is:  '.$Record->getBody(); //returns "The greatest hits from the best band in the world!"  
?>
登录后复制

重点在于: __call 和 create_function

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号