Home > Article > Backend Development > PHP custom function implements assign() array assignment to template and extract() variable assignment to template
This article mainly introduces the PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. It can simulate the template variable assignment function in the tp framework and involves PHP object-oriented array assignment. For operational skills, friends in need can refer to
This article describes the example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. Share it with everyone for your reference, the details are as follows:
Here simulates the tp framework template variable allocation and assignment operations.
extract($arr);
//The role of extract: import variables from the array into the current symbol table, the keys are used as variables, and the values are used as values! compact();
// — Create an array, including variable names and their values
class base{ public $array; public $key; public $val; public function assign($key,$val){ if(array($val)){ $this->array["$key"] = $val; }else{ $this->array["$key"] = compact($val); } } public function display($tpl){ $this->assign($this->key,$this->val); extract($this->array); if(file_exists($tpl)){ //模板存在就加载文件。 include $tpl; } } } class indexcontroller extends base{ public function index(){ $arr = array('a'=>'aaaaaaa','b'=>array('a'=>'111111','b'=>'22222','c'=>'3333'),'c'=>'ccccccc','d'=>'dddddd','e'=>'eeeee'); $str = '我是字符串'; $this->assign('arr',$arr); $this->assign('str',$str); $this->display('index.html'); } } $base = new base; $base->index();
Related recommendations:
php strftime function to obtain date and time (switch usage)
The above is the detailed content of PHP custom function implements assign() array assignment to template and extract() variable assignment to template. For more information, please follow other related articles on the PHP Chinese website!