php执行字符串代码
                        
                                                            php
                                                    
                         编程进阶笔记
                    
                    
                        
                             发布日期
                            2023-02-18 
                        
                        
                             更新日期
                            2023-05-10 
                        
                        
                             阅读次数 365
                        
                        
                             文章字数 1.5k
                        
                    
                最简单的办法,可以直接使用 eval 函数,但是这个函数总是发出警告。
还可以先把字符串,保存到一个文件中,然后 include 包含这个文件,但是这样又比较浪费性能。
所以自定义一个文件协议,更优雅的实现执行字符串类型的php代码。
<?php
//注册协议
stream_wrapper_register("exePHPStr", "VariableStream");
//执行php字符串【全局变量】
$GLOBALS['myStr'] = "<?php echo 11;";
include("exePHPStr://global.myStr");
echo "<br>";
//修改数据,再次执行
$GLOBALS['myStr'] = "<?php echo 22;";
include("exePHPStr://global.myStr");
echo "<br>";
echo "<br>";
//从数据库读取并执行
include("exePHPStr://table.test.6");
echo "<br>";
//再次读取另一张表的另一个id
include("exePHPStr://table.test2.7");
/**
 * 读取指定表指定id的数据
*/
function table_get(string $tableName,int $tableId) {
	return '<?php $i = "表'.$tableName.'的'.$tableId.'数据";
	echo "hello $i 
";';
}
//自定义协议
class VariableStream {
    private $string;
    private $position;
    
    public function stream_open($path, $mode, $options, &$opened_path) {
        $url = parse_url($path);
        $scheme = $url['scheme'];
        if($scheme!="exePHPStr"){
            throw new Exception("仅支持协议名称exePHPStr");
        }
        $host = $url["host"];
        $nodes = explode(".",$host);
        if(count($nodes) < 2){
            throw new Exception("执行phpStr参数错误");
        }
        $type = $nodes[0];
        if($type=="table"){ //读取数据表
            if(count($nodes)<3){
                throw new Exception("执行phpStr参数错误");
            }
            $tableName = $nodes[1];
            $tableId = $nodes[2];
            $this->string = table_get($tableName,$tableId);
        }
        elseif($type=="global"){
            $names = $nodes[1];
            if(!isset($GLOBALS[$names])){
                throw new Exception("不存在的GLOBAL参数:$names");
            }
            $this->string = $GLOBALS[$names];
        }
        else{
            throw new Exception("不支持的类型");
        }
        $this->position = 0;
        return true;
    }
    public function stream_read($count) {
        $ret =  substr($this->string, $this->position, $count);
        $this->position += strlen($ret);
        return $ret;
    }
    public function stream_eof() {}
    public function stream_stat() {}
    
    public function stream_set_option(){}
}
            文章作者: 朱丰华
文章链接: https://smart.52dixiaowo.com/blog/post-368.html
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。
                
                                            php
                                    
                
                    
                    
                    
                    
                
            
            发表评论
相关推荐