PHP constants use a few places to note (be cautious about using constants in PHP), constants php_php Tutorials

Source: Internet
Author: User
Tags define function php class php define zend framework

PHP constants use a few places to note (be cautious about using constants in PHP), constant PHP


Why use constants in PHP with caution?

The Zend Framework document reads: Constants contain numeric alphabetic characters and underscores, and numbers are allowed as constant names. All letters of a constant name must be capitalized. A class constant must be defined by "const" as a member of a class, and a global constant defined with "define" is strongly discouraged.

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

Let's analyze it together.

1. Define easy to produce unexpected errors

PHP constants cannot be modified and re-assigned after they are defined. But what happens if I assign a value again?

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

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

2. How can I tell if a PHP constant is defined? The judgment method is easy to write wrong

<?php define (' C ', 12345); Error Method 1, often committed if (Isset (c)) {...}//Error method 2, often committed if (defined (c)) {...}//correct method if (defined (' C ')) {...}? >

3. Low execution efficiency

<?php  define (' Forum_theme ', $forum [' THEME ']);   $this->display ('/'). Forum_theme. ' @Public: Login ');   The system will look for forum_theme?> from the entire execution process

Because PHP handles constants for multiple lookups, it is inefficient.

Summary: The problem with PHP constants is that the method of handling constants in PHP is too loose, if you can be strict, you will avoid a lot of problems. In the actual process, you can use the variable to not use the constant, because the variable efficiency is more convenient to use.

Therefore, if you are not using 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;  }}? >

function 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, the constructor name of the subclass is the same as the subclass name, and the constructor of the parent class in the subclass is not 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 for parent class ()]

In PHP 5.0 or later, unified use of construct () as a constructor, but still compatible with the 4.0 version of the constructor definition rules. If a 4.0 constructor and construct () function are defined at the same time, the construct () function takes precedence.

Replace/r/n with PHP eol for line breaks

When you write a program, you often use line breaks, with PHP built-in constant php_eol for line breaks.

A small line-of-exchange, different implementations on different platforms. In the UNIX world to replace the line with \ n instead, but windows in order to reflect his differences, using \ r \ n, more interesting is in the Mac with the \ R. Therefore the UNIX series uses the \n,windows series with the \r\n,mac to use the \ r.

As a result, the system will be converted to different lines depending on the platform system. If you want to wrap in a browser, use the PHP_EOL variable to break the line


How do I use PHP constants?

1. Constants are generally capitalized
Define (' MyStr ', "my constant");
2. Constants cannot be defined repeatedly, and are a fixed value. Relative variables

In the output of a constant in PHP, what is the use of constant (), this function?

Constants indicate that they do not change, but only once, constants are defined with the Define function: Define (' PAI ', 3.14);

ECHO constant (' PAI '); Effect equals: Echo PAI;

So generally not constant function

http://www.bkjia.com/PHPjc/877513.html www.bkjia.com true http://www.bkjia.com/PHPjc/877513.html techarticle PHP Constants Use a few places to be aware of (be cautious about using constants in PHP), why is constant PHP prudent to use the constants in PHP? The Zend Framework document reads: constants contain numeric characters ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.