Home > Article > Backend Development > Several things to note when using PHP constants (use constants in PHP with caution), constants php_PHP tutorial
Why should you use constants in PHP with caution?
Zend Framework documentation writes: Constants contain alphanumeric characters and underscores, and numbers are allowed as constant names. All letters in constant names must be capitalized. Class constants must be defined as members of the class via "const", and the use of "define"-defined global constants is strongly discouraged.
As the official framework of PHP, why is there such a requirement?
Let’s analyze it together.
1. define is prone to unexpected errors
PHP constants cannot be modified or assigned again after they are defined. But what happens if it is assigned again?
<?php define('C', 12345); define('C', 123); ?>
This code will report a notice error. The consequence is: before you define it, if someone else defines a constant with the same name, you may not really know what value it contains.
2. How to determine whether a PHP constant is defined? It is easy to make mistakes when writing the judgment method
<?php define('C', 12345); // 错误方法1,经常犯 if (isset(C)){……} // 错误方法2,经常犯 if (defined(C)){……} // 正确方法 if (defined('C')){……} ?>
3. Low execution efficiency
<?php define('FORUM_THEME',$forum['theme']); $this->display('/'.FORUM_THEME.'@Public:login'); // 系统会从整个执行流程中查找FORUM_THEME ?>
Because PHP needs to perform multiple searches when processing constants, the efficiency is low.
Summary: The problem with PHP constants is that PHP’s method of dealing with constants is too loose. If it can be stricter, many problems will be avoided. In the actual process, do not use constants if you can use variables, because variables are more efficient and more convenient to use.
So if you have to use constants or class variables, you can use the following methods:
<?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; } } ?>
What happens when the class name and function name are the same
In PHP 4, the constructor of a class needs to be the same as the class name, and the constructor name of a subclass must be the same as the subclass name. The constructor of the parent class will not be automatically executed in the subclass. To execute the constructor of the parent class in a subclass, you must execute a statement similar to the following:
$this->[Constructor name of parent class ()]
In PHP 5.0 and above, construct() is uniformly used as the constructor, but it is still compatible with the constructor definition rules of version 4.0. If both the 4.0 constructor and the construct() function are defined, the construct() function takes precedence.
Use PHP EOL to replace /r/n for line break
Line breaks are often used when writing programs. Use PHP’s built-in constant PHP_EOL to perform line breaks.
A small line break, which has different implementations on different platforms. In the unix world, n is used to replace line breaks, but in order to reflect the difference, windows uses rn. What is more interesting is that r is used in mac. Therefore, the unix series uses n, the windows series uses rn, and the mac uses r.
Therefore, the system will convert into different line breaks depending on the platform system. If you want to wrap the line in the browser, you need to use the PHP_EOL variable to wrap the line
1. Constants are generally capitalized
define('MYSTR', "My constant");
2. A constant cannot be defined repeatedly and is a fixed value. Relative to variables
Constant representation will not change and can only be assigned once. The definition of a constant is using the define function: define('PAI', 3.14);
echo constant('PAI'); The effect is equal to: echo PAI ;
So the constant function is generally not used