为什么要谨慎使用PHP中的常量?
Zend Framework文档中写道:常量包含数字字母字符和下划线,数字允许作为常量名。 常量名的所有字母必须大写。类常量必须通过 "const" 定义为类的成员,强烈不鼓励使用 "define" 定义的全局常量。
作为PHP的官方框架,为什么会有这样的要求?
让我们一起分析一下吧。
1. define容易产生意想不到的错误
PHP常量是定义后就不能修改和再次赋值。但是如果再次赋值会怎么样?
<?php define('C', 12345); define('C', 123); ?>
这段代码会报个notice错误。带来的后果是:在你定义之前,其它人要是定义了同名的常量,你可能真的不知道里面究竟是什么值。
2. 如何判断PHP常量是否被定义?判断方法易写错
<?php define('C', 12345); // 错误方法1,经常犯 if (isset(C)){……} // 错误方法2,经常犯 if (defined(C)){……} // 正确方法 if (defined('C')){……} ?>
3. 执行效率低
<?php define('FORUM_THEME',$forum['theme']); $this->display('/'.FORUM_THEME.'@Public:login'); // 系统会从整个执行流程中查找FORUM_THEME ?>
因为php处理常量的时候要进行多次查找,所以效率低。
总结:PHP常量的问题,在于PHP处理常量的方法过于宽松导致的,如果能够严格一些,就会避免很多的问题。在实际过程,能用变量就不要用常量,因为变量的效率高使用更加方便。
因此若非要使用常量或者类变量,可使用以下方法:
<?php class foo { const WEBSITE = "www.zhuyinghao.com"; protected $_forum_theme; function name() { echo WEBSITE; $this->_forum_theme = $forum['theme']; } function displace() { echo $this->_forum_theme; } } ?>
类名和函数名相同时的作用
在PHP 4中,类的构造函数需要和类名相同,子类的构造函数名与子类名相同,在子类里父类的构造函数不会自动执行。要在子类里执行父类的构造函数,必须执行类似以下语句:
$this->[父类的构造函数名()]
在 PHP 5.0 以上版本里,统一使用construct()作为构造函数,但仍兼容了 4.0 版本的构造函数的定义规则。如果同时定义了4.0的构造函数和 construct()函数,则construct() 函数优先。
用PHP EOL来替换/r/n进行换行
写程序时会经常用到换行,用PHP内置常量PHP_EOL来进行换行。
一个小小的换行,在不同的平台有着不同的实现。在unix世界换行就用\n来代替,但是windows为了体现他的不同,就用\r\n,更有意思的是在mac中用\r。因此unix系列用 \n,windows系列用 \r\n,mac用 \r。
因此系统会根据平台系统的不同,转换成不同的换行。如果要在浏览器中换行,就要使用PHP_EOL变量进行换行

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Alipay 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

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft