PHP Variable vs Array vs Object

前端 未结 7 1320
忘掉有多难
忘掉有多难 2020-12-31 18:03

This is probably considered a really silly question, but I\'m in the process of putting together a simple template system for a website and am trying to keep track of my var

7条回答
  •  生来不讨喜
    2020-12-31 18:54

    Arrays

    I would suggest on the backend part, keep everything stored in an array. This allows you to have only one variable to keep track of, and once you pass it to the frontend, you can extract() the array, to convert them into simple variables.

    Syntax

    Using extract() simplifies the syntax on the FrontEnd, which means you will only always have $varibles in the template.

    On the backend you would set
    $array['title'];

    Which once extracted would in the template be
    $title;

    Example of a backend function

     protected function fetch($template, $data = null)
        {
            if (!$this->isTemplate($template)) {
                throw new Exception("Template file $this->_templatePath$template not found");
            }
            ob_start();
            if (is_array($data)) {
                extract($data, EXTR_SKIP);
            }
            require $this->_templatePath . $template . EXT;
            return ob_get_clean();
        }
    

提交回复
热议问题