php命名空间 和 独立的模板引擎 的必要性 在哪里?
php引入命名空间用"\"分割的语法,算是独创,初用非常不习惯;且,用设计良好的目录结果来管理类,也能实现命名空间管理,个人认为新引入的namespace,used等都是画蛇添足,望指教交流。
再说模板引擎,个人认为模板引擎唯一用处在于:把写界面(模板)和写业务逻辑分离开,使的美工可以独立开发模板,以减少程序员的工作量。但对于美工来说,学习 类似{var}的模板标签,不亚于写php代码直接填充模板的难度;且非php语法的模板需要模板编译的过程,消耗性能。除此之外,想请问各种模板引擎的好处在哪里?
请不要简单的回答:“你的项目规模不够大,并行开发人员不够多”
回复内容:
php引入命名空间用"\"分割的语法,算是独创,初用非常不习惯;且,用设计良好的目录结果来管理类,也能实现命名空间管理,个人认为新引入的namespace,used等都是画蛇添足,望指教交流。
再说模板引擎,个人认为模板引擎唯一用处在于:把写界面(模板)和写业务逻辑分离开,使的美工可以独立开发模板,以减少程序员的工作量。但对于美工来说,学习 类似{var}的模板标签,不亚于写php代码直接填充模板的难度;且非php语法的模板需要模板编译的过程,消耗性能。除此之外,想请问各种模板引擎的好处在哪里?
请不要简单的回答:“你的项目规模不够大,并行开发人员不够多”
命名空间用得不多,跳过,就说说模板引擎吧。
关于模板引擎的使用,其实是回到最原始的需求:无非就是希望尽可能方便地输出html。
虽然php与其他很多语言不一样,它最初被设计出来的时候就是为了方便在一堆html里头实现一些动态特性(所以其实也有很多项目的确是用php来实现模板的,比如wordpress就是一堆php);但是和smarty(之类的模板引擎)比起来,在直接写html的时候,的确大多数时候还是smarty更方便,因为smarty引擎是针对编写html专门优化的。比如说在smarty里头需要将一个变量进行html转义:
<code>{$xxx|escape:'html'} </code>
php呢?
<code><?php echo htmlspecialchars($xxx); ?> </code>
如果改改成引号转义,smarty只需要把 html 改成 quotes 就行了,形式上保持了一致,在编写和阅读上都比较舒服。至于php,当然把 htmlspecialchars
改成 addslashes
也可以,但是php的函数,真的很难用(又难记又难写,很多类似的一组函数,变量的顺序也没有统一的约定,谁用谁恶心)……关于这一点我深有体会,10年的时候自己写的一个项目,觉得不是很有必要用smarty,于是自己用php来实现,写得非常痛苦,如果当时对smarty了解多一点(只是听说过,感觉好像比较重量级就没用),就绝对不可能出现这种情况。
最近webpy用的比较多,webpy的模板引擎我觉得比smarty用起来更爽的一点也在html转义:它默认就是转义的,需要在页面引用变量的时候,直接 $xxx
就行了。如果不需要转义也很简单: $:xxx
。
这些专门为了html开发而优化的特性,对于php这种一开始就很随意、以后也是很随意地乱堆起来的语言来说,的确是要好很多。
而关于编译、效率的问题:其实大多数模板引擎都是有编译缓存的,只是在第一次访问的时候需要编译,此后带来的开销几乎可以忽略不计。关于效率的问题,其实还可以顺便吐槽一下php——本来效率就不怎么样,这一点开销真的没什么。(p.s. 我快成php黑了)
use等等是命名空间的核心功能啊,显然,这是从C++等语言参考过来的。
命名空间的目的是为了加强标识符的管理,并非所有情况下都要使用非常长的完整限定名,所以要有use等机制来对标识符的命名空间部分进行简写。
当然,PHP的命名空间确实设计的不怎么样,比如奇葩的反斜杠,对函数和变量功能有限等等。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
