Several Notices for Constant Use in PHP of Use Constants in PHP Carefully

  • 2021-07-18 07:43:12
  • OfStack

Why use constants in PHP with caution?

The Zend Framework document says that constants contain alphanumeric characters and underscores, and numbers are allowed as constant names. All letters of the constant name must be capitalized. Class constants must be defined as members of the class through "const", and the use of global constants defined by "define" is strongly discouraged.

As the official framework of PHP, why is there such a requirement?

Let's analyze it one by one.

1. define is prone to unexpected errors

PHP constants are defined and cannot be modified and re-assigned. But what happens if you assign it again?


<?php
 define('C', 12345);
 define('C', 123);
?>

This code will report an notice error. The consequence is that if someone else defines a constant with the same name before you define it, you may not really know what value is inside it.

2. How do I determine if the PHP constant is defined? The judgment method is easy to write mistakes


<?php
 define('C', 12345);
 //  Wrong method 1 , frequent offenders 
 if (isset(C)){ ... }
 //  Wrong method 2 , frequent offenders 
 if (defined(C)){ ... }
 //  Correct method 
 if (defined('C')){ ... }
?>

3. Inefficient implementation


<?php
  define('FORUM_THEME',$forum['theme']); 
  $this->display('/'.FORUM_THEME.'@Public:login'); 
  //  The system will find it from the whole execution process FORUM_THEME
?>

Because php has to look up many times when processing constants, it is inefficient.

Summary: The problem of PHP constant is that PHP's method of dealing with constant is too loose. If it can be strict, many problems will be avoided. In the actual process, you should not use constants if you can use variables, because the efficiency of variables is high and it is more convenient to use them.

Therefore, if you don't want 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 the class needs to be the same as the class name, and the constructor name of the subclass needs to be the same as the subclass name, in which 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 like the following:

$this- > [Parent class constructor name ()]

In PHP 5.0 and above, construct () was used 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.

Replace the/r/n with PHP EOL for line wrapping

Write the program will often use the newline, with PHP built-in constant PHP_EOL for newline.

A small line break has different implementations on different platforms. In the world of unix, the newline is replaced by\ n, but in order to reflect its difference, windows uses\ r\ n, and more interestingly, it uses\ r in mac. Therefore, unix series is used\ n, windows series is used\ r\ n, and mac is used\ r.

Therefore, the system will convert into different line breaks according to different platform systems. If you want to wrap lines in the browser, use the PHP_EOL variable to wrap lines


Related articles: