目录
下载和授权
一些关于模板引擎的背景知识
模板引擎基础知识
别样的解决方案
例子
=$title;?>
User List
缓存
设置多个变量
相关的链接
模板类源代码
首页 后端开发 php教程 超越模板引擎_PHP

超越模板引擎_PHP

Jun 01, 2016 pm 12:42 PM
php the 引擎 文件 模板 超越

总体来说,模板引擎是一个"好东西"

作为一个PHP/Perl的程序员,许多模板引擎(fastTemplate, Smarty, Perl的 HTML::Template)的用户,以及我自己的(bTemplate [1] 的作者),我讲这句话很多次了。

然而,在同事进行了长时间的讨论之后,我确信了大量的模板引擎(包括我自己写的)根本是错误的。 我想唯一的例外是Smarty [2],虽然我认为它太庞大了,并且考虑到这篇文章的其余部分相当的没有观点。然而,就你为什么选择Smarty(或者类似的解决方案)有几个理由,这些将在文章后面探究。

这篇文章讨论模板的理论。我们将看到为什么大部分"模板引擎"是过于肥大,并且最终我们将回过头来看一个轻量级的,小巧快速的另类选择。


下载和授权

模板类和所有在本文中使用的例子能够在这里下载:template.zip [3]。你可以根据发布 [4]在 OSI [5] 的 MIT Open Source License使用这些文件中的代码。


一些关于模板引擎的背景知识

让我们首先研究一下模板引擎的背景知识。模板引擎被设计出来用于把商业逻辑(例如从数据库中获取数据或者计算贸易耗费)从数据的表现分离开来。模板引擎解决了两个主要问题:

  1. 如何实现这种分离
  2. 如何从HTML中分离"复杂"的php代码

这从理论上使得没有PHP经验的HTML设计者能够不看任何PHP代码的条件下修改站点的外观。

然而,模板系统也引入了一些复杂性。首先,我们现在有一个从多个文件得来的"页面"。典型的,你可能有一个主PHP页负责业务逻辑,一个外面的"布局"模板把整个站点的整体布局进行渲染,一个内部的内容特定的模板,一个数据库抽象层,以及模板引擎本身(这些可能是也可能不是由多个文件组成)。也有可能,一些人仅仅简单地在每个PHP页面的首尾处包含"头部"和"尾部"文件。

这产生的单个页面的文件数量是很可观的。然而,因为PHP解析器非常快,用到的文件数量可能不是那么重要除非你的站点流量很大。
然而,要记住模板系统引入了另外一个处理的层次。模板文件不仅仅是必须被包含,他们还必须被解析(取决于模板系统,这个行为有很多种方式来完成 —— 使用正则表达式,字符串替换,编译,词法分析,等等)。这就是为什么对模板进行测速变得流行起来:因为模板引擎使用各种方法来解析数据,它们中的一些比另外一些要快(而且,一些模板引擎提供了比其他引擎更加丰富的功能)。


模板引擎基础知识

简单地说,模板引擎利用了用C写的脚本语言(PHP)。在这些嵌入的脚本语言中,你有另外一个伪脚本语言(无论你的模板引擎支持何种标签)。某些提供了简单的变量改写和循环。另外一些呢,则提供了条件和嵌套循环。而再其他的呢(至少有Smarty)提供了一个PHP的比较大的子集的接口,以及一个缓冲层。

为什么我认为Smarty最接近于正确的方向?因为Smarty的目标是"把业务逻辑从表现中分离出来"而不是"PHP代码和HTML代码的分离"。这看上去区别不大,但是它正是要点所在。任何模板引擎的最终目标不应该是从HTML移除所有的逻辑。它应该是把表现逻辑从业务逻辑中分离出来。

有很多你仅仅需要逻辑来正确显示你的数据的例子。例如,你的业务逻辑是从你的数据库中获取一个用户列表。你的表现逻辑可能是把用户列表用3列显示。可能修改用户列表函数使得它返回3个数组是很笨的办法。毕竟函数不应该关心数据接下来要怎么处理这样的事情。然而,在你的模板文件中缺少一些逻辑,那些正是你要做的事情。

在这点上Smarty是正确的(使得你利用PHP的很多东西),但是仍然有许多问题。基本上,它仅仅提供了一个以新语法访问PHP的接口。以那开始,它看上去不那么聪明了。是不是事实上写 {foreach --args} foreach --args ?> 更加简单?如果你认为这样简单一些,问问你自己是不是在包含一个巨大的模板库来到成这种分离时能够看到真正的意义要更加简单一些。诚然,Smarty提供了许多其他很好的特性,但是看上去这些益处能够在不用承担包含Smarty类库的情况下也能获得。


别样的解决方案

我主要要鼓吹的一个解决方案是一个使用PHP代码作为它的原生脚本语言的"模板引擎"。我知道这以前有人做过。而且当我第一次看到的时候,我想,"为什么要这样做?",然而我在考虑过我同事的论据之后,并且实现了一个直接使用PHP代码仍然实现了把业务逻辑和表现逻辑分离的最终目标的模板系统时(只用了大约25行代码,不包括注释),我意识到了好处所在。

这个系统给像我们这样的开发者提供了对PHP核心函数的访问权利,我们能够使用他们来格式化输出——像日期格式化这样的任务应该在模板中处理。而且,因为模板是普通的PHP文件,像Zend Performance Suite [6] 和PHP Accelerator [7] 这样的字节码缓存程序,能够自动缓存模板(因而,它们不需要在每次被访问时都被重新解释执行)。只要你记得把你的模板文件命名为程序能够辨认出是PHP文件的名字(通常,你仅仅需要确保它们有一个.php的后缀),这确实是一个好处。

当我认为这种方法比经典的模板引擎要高明得多时,肯定还有一些要商榷的问题。最明显的反面意见是,PHP代码太复杂了,而且设计者不应该强迫去学习PHP。事实上,PHP代码和像Smarty这样的高级模板引擎的语法差不多简单(如果不是更简单的话)。而且,设计者能够使用像=$var;?>这样的简写PHP。这要比{$var}复杂很多?当然,这要长一些,但是如果你习惯了,你能够获得了PHP的威力而且不用承受解析模板文件带来的负担。

第二,而且可能更重要的,在基于PHP的模板中没有固有的安全。Smarty提供了选项在模板文件中彻底禁用PHP代码。它使得开发者能够约束模板能够访问的函数和变量。如果你没有不怀好意的设计者,这不会是什么问题。然而,如果你允许外部的用户上传或者修改模板,我在此展示的基于PHP的解决方案绝对没有任何安全可言!任何代码都能放入模板中并且得到运行。是的,甚至是一个print_r($GLOBALS)(这将改有恶意的用户访问脚本中任何变量的权利)。

但是,我个人或者工作上写过的项目中,绝大多数不允许最终的用户修改或者上传模板。如果是这样,问题就不存在了。因此现在让我们来看看代码吧。


例子

这是一个简单的用户列表页面的例子。

<?php <br /> require_once('template.php');  <br> <br> /**  <br> * This variable holds the file system path to all our template files.  <br> */  <br> $path = './templates/';  <br> <br> /**  <br> * Create a template object for the outer template and set its variables.  <br> */  <br> $tpl = & new Template($path);  <br> $tpl->set('title', 'User List');  <br> <br> /**  <br> * Create a template object for the inner template and set its variables.  The  <br> * fetch_user_list() function simply returns an array of users.  <br> */  <br> $body = & new Template($path);  <br> $body->set('user_list', fetch_user_list());  <br> <br> /**  <br> * Set the fetched template of the inner template to the 'body' variable in  <br> * the outer template.  <br> */  <br> $tpl->set('body', $body->fetch('user_list.tpl.php'));  <br> <br> /**  <br> * Echo the results.  <br> */  <br> echo $tpl->fetch('index.tpl.php');  <br> ?>

其中有两个值得注意的重要的概念。第一个就是内部和外部模板的概念。外部模板包含定义站点主要外观的HTML代码。而内部模板包含定义站点内容区域的HTML代码。当然,你能够在任意数目的层上有任意数目的模板。因为通常我们给每个区域使用不同的模板对象,所以没有名字空间的问题。例如,我能在内部和外部模板中都有变量叫"title",而不用害怕有什么冲突。

这是一个用来显示用户列表的模板的简单例子。注意特殊的foreach和endforeach;语法在PHP手册中有说明 [8]。它完全是可选择的。

而且,你可能奇怪我为什么要用.php的后缀来命名我的模板文件。呵呵,许多PHP字节码缓存解决方案(比如 phpAccelerator)如果要被认成PHP文件,需要文件有一个.php后缀。因为这些模板是PHP文件,为什么不去获得这些好处?

<table>  <br>    <tr>  <br>        <th>Id</th>  <br>        <th>Name</th>  <br>        <th>Email</th>  <br>        <th>Banned</th>  <br>    </tr>  <br> foreach($user_list as $user): ?>  <br>    <tr>  <br>        <td align="center">=$user['id'];?></td>  <br>        <td>=$user['name'];?></td>  <br>        <td><a href="mailto:<?=%24user%5B'email'%5D;?>">=$user['email'];?></a></td>  <br>        <td align="center">=($user['banned'] ? 'X' : ' ');?></td>  <br>    </tr>  <br> endforeach; ?>  <br> </table>

这个layout.tpl.php是一个简单的例子(定义了整个页面看上去是什么样子的模板文件)

<br>     <br>        <title>=$title;?></title> <br>     <br> <br>     <br> <br>        <h2 id="title-gt">=$title;?></h2> <br> <br> =$body;?> <br> <br>     <br>

而这是解析后的输出。

<br>   <br>    <title>User List</title> <br>   <br> <br>   <br> <br>    <h2 id="User-List">User List</h2> <br> <br> <table> <br>  <tr> <br>    <th>Id</th> <br>    <th>Name</th> <br>    <th>Email</th> <br>    <th>Banned</th> <br>  </tr> <br>  <tr> <br>    <td align="center">1</td> <br>    <td>bob</td> <br>    <td><a href="mailto:bob@mozilla.org">bob@mozilla.org</a></td> <br>    <td align="center"> </td> <br>  </tr> <br>  <tr> <br>    <td align="center">2</td> <br>    <td>judy</td> <br>    <td><a href="mailto:judy@php.net">judy@php.net</a></td> <br>    <td align="center"> </td> <br>  </tr> <br>  <tr> <br>    <td align="center">3</td> <br>    <td>joe</td> <br>    <td><a href="mailto:joe@opera.com">joe@opera.com</a></td> <br>    <td align="center"> </td> <br>  </tr> <br>  <tr> <br>    <td align="center">4</td> <br>    <td>billy</td> <br>    <td><a href="mailto:billy@wakeside.com">billy@wakeside.com</a></td> <br>    <td align="center">X</td> <br>  </tr> <br>  <tr> <br>    <td align="center">5</td> <br>    <td>eileen</td> <br>    <td><a href="mailto:eileen@slashdot.org">eileen@slashdot.org</a></td> <br>    <td align="center"> </td> <br>  </tr> <br> </table> <br>   <br>


缓存

因为解决方案简单如斯,实现模板缓存成为了一个非常简单的任务。为了实现缓存,我们有一个二级类,它扩展了原来的模板类。CachedTemplate类事实上使用和原来的模板类相同的API。不同点是我们必须传递缓存的设置给构造函数,并且调用fetch_cache()而不是fetch()

缓存的概念是简单的。简单的说,我们设置一个缓存时间来调表输出应该被保存的时长(以秒为单位)。在产生一个页面的所有工作开展之前,我们必须首先测试页面是否已经被缓存了,而且缓存是否仍然没有过期。如果缓存在这那,我们不需要在去麻烦数据库和业务逻辑来产生页面——我们可以简单地输出原先缓存地内容。

这种方法需要解决唯一地标识缓存文件的问题。如果一个站点是被一个显示基于GET变量的中心脚本所控制,对每个PHP文件只有一个缓存不会有什么帮助。例如,如果index.php?page=about_us和用户调用index.php?page=contact_us得到的显示完全不同。

问题是通过给每个页面产生一个唯一的cache_id来解决的。为了做到这个目的,我们把事实上被请求的文件变成REQUEST_URI(基本上就是整个URL:index.php?foo=bar&bar=foo)。当然,这个转换过程是受到CachedTemplate类控制的,但是要记住的重要的事情是你绝对要在创建CachedTemplate对象时传递一个唯一的cache_id。当然下面有例子来说明。

使用缓存包括以下步骤。

  1. include() 模板源文件

  2. 创建一个新的CachedTemplate对象(并且传递路径,唯一的cache_id和缓存过期时间给模板)

  3. 测试内容是否已经被缓存了

  4. 如果还促拿了,显示文件并且结束脚本

  5. 否则,进行所有的处理并且fetch()模板

  6. fetch_cache()的调用将自动产生一个新的缓存文件

这个脚本假定你的缓存文件将放到./cache/中,因此你必须创建那个目录并且改变它的目录权限(chmod)使得Web服务器能够写入文件。而且还要注意如果你在编写脚本的过程中发现了错误,错误也会被缓存!因而在你开发的过程中禁用缓存是一个好主意。最好的办法是给cache的生存周期传递0——这样,缓存总是立即就失效了。

这是一个实际的缓存的例子。

<?php <br /> /** <br> * Example of cached template usage.  Doesn't provide any speed increase since <br> * we're not getting information from multiple files or a database, but it <br> * introduces how the is_cached() method works. <br> */ <br> <br> /** <br> * First, include the template class. <br> */ <br> require_once('template.php'); <br> <br> /** <br> * Here is the path to the templates. <br> */ <br> $path = './templates/'; <br> <br> /** <br> * Define the template file we will be using for this page. <br> */ <br> $file = 'list.tpl.php'; <br> <br> /** <br> * Pass a unique string for the template we want to cache.  The template <br> * file name + the server REQUEST_URI is a good choice because: <br> *    1. If you pass just the file name, re-used templates will all <br> *       get the same cache.  This is not the desired behavior. <br> *    2. If you just pass the REQUEST_URI, and if you are using multiple <br> *       templates per page, the templates, even though they are completely <br> *       different, will share a cache file (the cache file names are based <br> *       on the passed-in cache_id. <br> */ <br> $cache_id = $file . $_SERVER['REQUEST_URI']; <br> $tpl = & new CachedTemplate($path, $cache_id, 900); <br> <br> /** <br> * Test to see if the template has been cached.  If it has, we don't <br> * need to do any processing.  Thus, if you put a lot of db calls in <br> * here (or file reads, or anything processor/disk/db intensive), you <br> * will significantly cut the amount of time it takes for a page to <br> * process. <br> * <br> * This should be read aloud as "If NOT Is_Cached" <br> */ <br> if(!($tpl->is_cached())) { <br>    $tpl->set('title', 'My Title'); <br>    $tpl->set('intro', 'The intro paragraph.'); <br>    $tpl->set('list', array('cat', 'dog', 'mouse')); <br> } <br> <br> /** <br> * Fetch the cached template.  It doesn't matter if is_cached() succeeds <br> * or fails - fetch_cache() will fetch a cache if it exists, but if not, <br> * it will parse and return the template as usual (and make a cache for <br> * next time). <br> */ <br> echo $tpl->fetch_cache($file); <br> ?>


设置多个变量

我们如何能够同时设置多个变量?这又一个使用由Ricardo Garcia贡献的函数的例子。

<?php <br /> require_once('template.php');  <br> <br> $tpl = & new Template('./templates/');  <br> $tpl->set('title', 'User Profile');  <br> <br> $profile = array(  <br>    'name' => 'Frank',  <br>    'email' => 'frank@bob.com',  <br>    'password' => 'ultra_secret'  <br> );  <br> <br> $tpl->set_vars($profile);  <br> <br> echo $tpl->fetch('profile.tpl.php');  <br> ?>

相关的模板是这样的:

<table cellpadding="3" border="0" cellspacing="1">  <br>    <tr>  <br>        <td>Name</td>  <br>        <td>=$name;?></td>  <br>    </tr>  <br>    <tr>  <br>        <td>Email</td>  <br>        <td>=$email;?></td>  <br>    </tr>  <br>    <tr>  <br>        <td>Password</td>  <br>        <td>=$password;?></td>  <br>    </tr>  <br> </table>

而且解析后的输出是这样的:

<table cellpadding="3" border="0" cellspacing="1"> <br>  <tr> <br>    <td>Name</td> <br>    <td>Frank</td> <br>  </tr> <br>  <tr> <br>    <td>Email</td> <br>    <td>frank@bob.com</td> <br>  </tr> <br>  <tr> <br>    <td>Password</td> <br>    <td>ultra_secret</td> <br>  </tr> <br> </table>

特别感谢Ricardo Garcia和Harry Fuecks他们的对这篇文章的贡献。


相关的链接

这儿是一个总体上探究模板引擎的好去处的列表。

  • Web Application Toolkit Template View [9] - 许多关于模板实现方法的信息
  • MVC Pattern [10] - 描述3层应用程序的设计
  • SimpleT [11] - 另一个使用PEAR::Cache_Lite的基于php的模板引擎
  • Templates and Template Engines [12] - 更多关于各种模板实现的信息
  • Smarty [13] - 编译型模板引擎

模板类源代码

以及最后出场的,模板类。

<?php <br /> /** <br> * Copyright © 2003 Brian E. Lozier (brian@massassi.net) <br> * <br> * set_vars() method contributed by Ricardo Garcia (Thanks!) <br> * <br> * Permission is hereby granted, free of charge, to any person obtaining a copy <br> * of this software and associated documentation files (the "Software"), to <br> * deal in the Software without restriction, including without limitation the <br> * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or <br> * sell copies of the Software, and to permit persons to whom the Software is <br> * furnished to do so, subject to the following conditions: <br> * <br> * The above copyright notice and this permission notice shall be included in <br> * all copies or substantial portions of the Software. <br> * <br> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR <br> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, <br> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE <br> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER <br> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING <br> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS <br> * IN THE SOFTWARE. <br> */ <br> <br> class Template { <br>    var $vars; /// Holds all the template variables <br>    var $path; /// Path to the templates <br> <br>    /** <br>     * Constructor <br>     * <br>     * @param string $path the path to the templates <br>     * <br>     * @return void <br>     */ <br>    function Template($path = null) { <br>        $this->path = $path; <br>        $this->vars = array(); <br>    } <br> <br>    /** <br>     * Set the path to the template files. <br>     * <br>     * @param string $path path to template files <br>     * <br>     * @return void <br>     */ <br>    function set_path($path) { <br>        $this->path = $path; <br>    } <br> <br>    /** <br>     * Set a template variable. <br>     * <br>     * @param string $name name of the variable to set <br>     * @param mixed $value the value of the variable <br>     * <br>     * @return void <br>     */ <br>    function set($name, $value) { <br>        $this->vars[$name] = $value; <br>    } <br> <br>    /** <br>     * Set a bunch of variables at once using an associative array. <br>     * <br>     * @param array $vars array of vars to set <br>     * @param bool $clear whether to completely overwrite the existing vars <br>     * <br>     * @return void <br>     */ <br>    function set_vars($vars, $clear = false) { <br>        if($clear) { <br>            $this->vars = $vars; <br>        } <br>        else { <br>            if(is_array($vars)) $this->vars = array_merge($this->vars, $vars); <br>        } <br>    } <br> <br>    /** <br>     * Open, parse, and return the template file. <br>     * <br>     * @param string string the template file name <br>     * <br>     * @return string <br>     */ <br>    function fetch($file) { <br>        extract($this->vars);          // Extract the vars to local namespace <br>        ob_start();                    // Start output buffering <br>        include($this->path . $file);  // Include the file <br>        $contents = ob_get_contents(); // Get the contents of the buffer <br>        ob_end_clean();                // End buffering and discard <br>        return $contents;              // Return the contents <br>    } <br> } <br> <br> /** <br> * An extension to Template that provides automatic caching of <br> * template contents. <br> */ <br> class CachedTemplate extends Template { <br>    var $cache_id; <br>    var $expire; <br>    var $cached; <br> <br>    /** <br>     * Constructor. <br>     * <br>     * @param string $path path to template files <br>     * @param string $cache_id unique cache identifier <br>     * @param int $expire number of seconds the cache will live <br>     * <br>     * @return void <br>     */ <br>    function CachedTemplate($path, $cache_id = null, $expire = 900) { <br>        $this->Template($path); <br>        $this->cache_id = $cache_id ? 'cache/' . md5($cache_id) : $cache_id; <br>        $this->expire   = $expire; <br>    } <br> <br>    /** <br>     * Test to see whether the currently loaded cache_id has a valid <br>     * corrosponding cache file. <br>     * <br>     * @return bool <br>     */ <br>    function is_cached() { <br>        if($this->cached) return true; <br> <br>        // Passed a cache_id? <br>        if(!$this->cache_id) return false; <br> <br>        // Cache file exists? <br>        if(!file_exists($this->cache_id)) return false; <br> <br>        // Can get the time of the file? <br>        if(!($mtime = filemtime($this->cache_id))) return false; <br> <br>        // Cache expired? <br>        if(($mtime + $this->expire)            @unlink($this->cache_id); <br>            return false; <br>        } <br>        else { <br>            /** <br>             * Cache the results of this is_cached() call.  Why?  So <br>             * we don't have to double the overhead for each template. <br>             * If we didn't cache, it would be hitting the file system <br>             * twice as much (file_exists() & filemtime() [twice each]). <br>             */ <br>            $this->cached = true; <br>            return true; <br>        } <br>    } <br> <br>    /** <br>     * This function returns a cached copy of a template (if it exists), <br>     * otherwise, it parses it as normal and caches the content. <br>     * <br>     * @param $file string the template file <br>     * <br>     * @return string <br>     */ <br>    function fetch_cache($file) { <br>        if($this->is_cached()) { <br>            $fp = @fopen($this->cache_id, 'r'); <br>            $contents = fread($fp, filesize($this->cache_id)); <br>            fclose($fp); <br>            return $contents; <br>        } <br>        else { <br>            $contents = $this->fetch($file); <br> <br>            // Write the cache <br>            if($fp = @fopen($this->cache_id, 'w')) { <br>                fwrite($fp, $contents); <br>                fclose($fp); <br>            } <br>            else { <br>                die('Unable to write cache.'); <br>            } <br> <br>            return $contents; <br>        } <br>    } <br> } <br> ?>

另外一个值得注意的重要的事情是这里展示的解决办法是我们传递模板的文件名给fetch()函数。如果你需要重用模板对象而不去re-set()所有的变量,这将比较有用。

并且记住:模板引擎的要点是把你的业务逻辑从你的表现逻辑中分离出来,而不是把你的PHP代码从HTML代码中分离出来。

本文附件下载:template.zip

[1] http://www.massassi.com/bTemplate/
[2] http://smarty.php.net/
[3] http://www.sitepoint.com/examples/tempeng/template.zip
[4] http://opensource.org/licenses/mit-license.html
[5] http://www.opensource.org/
[6] http://zend.com/store/products/zend-performance-suite.php
[7] http://www.php-accelerator.co.uk/
[8] http://www.php.net/manual/en/control-structures.alternative-syntax.php
[9] http://wact.sourceforge.net/index.php/TemplateView
[10] http://www.phppatterns.com/index.php/article/articleview/11/
[11] http://simplet.sourceforge.net/
[12] http://phppatterns.com/index.php/article/articleview/4/1/1/
[13] http://smarty.php.net/

本文英文原版地址:http://www.sitepoint.com/article/1218/
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

我后悔之前不知道的 7 个 PHP 函数 我后悔之前不知道的 7 个 PHP 函数 Nov 13, 2024 am 09:42 AM

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? 什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

See all articles