PHP Custom Function Implementation assign () array assigned to template and extract () variable assignment to template function example

Source: Internet
Author: User
Tags compact
This article mainly introduces PHP custom function implementation assign () array assigned to template and extract () variable assignment to template function, can simulate the template variable allocation function in TP frame, involving PHP based on object-oriented array assignment related operation skills, need friends can refer to the next

This example describes the PHP custom function implementation assign () array assigned to templates and extract () variables assigned to the template function. Share to everyone for your reference, as follows:

This simulates the TP frame template variable assignment and assignment operation.

extract($arr);Extract function: Import variables from the array into the current symbol table, the key to do the variable, value to do value!
compact();-Create an array, including the 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)) {//template exists on the load file.      include $tpl;}}}  Class Indexcontroller extends base{public  function Index () {    $arr = array (' a ' = ' = ' aaaaaaa ', ' B ' =>array (' A ' = ' 111111 ', ' b ' = ' 22222 ', ' c ' = ' 3333 '), ' c ' = ' CCCCCCC ', ' d ' and ' dddddd ', ' e ' = ' eeeee ');    $str = ' I am a string ';    $this->assign (' arr ', $arr);    $this->assign (' str ', $str);    $this->display (' index.html ');}  } $base = new Base; $base->index ();

Articles you may be interested in:

Analysis of basic design idea and realization method of MVC framework based on PHP imitation TP

Yii2 Installing the detailed process _php instance

CI Framework (CodeIgniter) Implementation of database additions and deletions to the operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.