Several issues that need to be paid attention to when using PHP constants

伊谢尔伦
Release: 2023-03-11 22:34:01
Original
1385 people have browsed it

Constant contains 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 for global constants is strongly discouraged

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 us 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?

Copy after login

This code will report a notice error. The consequence is: before you define it, if other people define a constant with the same name, you may really not know what the value is inside.

2. How to determine whether a PHP constant is defined? The judgment method is easy to write incorrectly

Copy after login

3. Low execution efficiency

display('/'.FORUM_THEME.'@Public:login'); 
  // 系统会从整个执行流程中查找FORUM_THEME
?>
Copy after login

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 handling 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 method:

_forum_theme = $forum['theme'];
  }
  function displace() 
  {
    echo $this->_forum_theme;
  }
 }
?>
Copy after login

The effect when the class name and function have the same name

In PHP 4, the constructor of a class needs to be the same as the class name. The constructor name of the subclass is the same as the subclass name. In the subclass, the constructor of the parent class will not be automatically executed. To execute the constructor of the parent class in a subclass, you must execute a statement similar to the following:

$this->[Constructor name of the parent class ()]

In PHP 5.0 or above Here, construct() is used uniformly as the constructor, but it is still compatible with the definition rules of the constructor in 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 for line breaks.

A small line break 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 \r\n. What is more interesting is that \r is used in Mac. Therefore, use \n for unix series, \r\n for windows series, and \r for mac.

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

The above is the detailed content of Several issues that need to be paid attention to when using PHP constants. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!