Custom constants and system constants used by the thinkphp

Source: Internet
Author: User
Thinkphp the use of custom constants and system constants

Constants we all know is to define the amount of good one constant, in PHP the definition of the rule is define ("Constant name", "Constant value");

That is, in all the pages can go to use this constant simple definition of a few constants do not have any meaning, it is important to use the constant area optimization code, I said the optimization code is to improve the maintainability of code, and sometimes to simplify the role of code, but this is definitely not the main purpose.

In the traditional code writing, we are using direct writing, such as URL address, these things although directly go to some can go to achieve function but, in the process of late maintenance of the program, first got very difficult, although said can take the use of replacement function, but this is not a permanent, And there is a better way than this, is to define constants to use, so in the later maintenance, if it is to change the disk or variable information, directly modify the variable can be, although the reason is very simple, but can be flexible use is not simple.

In addition to the custom constants, there are some system constants, if you want to see these constant information, you can use the following code to implement:

echo "

";
Var_dump (Get_defined_constants ());
echo "
";

You'll get stuck in a bunch of things, inside is all the system constants provide you to use, but the point is, whether it is our custom constants or system constants, the more commonly used in the back of the piece, this is to note:

Here is an example code for the constants I define, taking the think template as an example

Define the website domain constant define ("Set_url", "http://shop.com/");//define the css,img of the foreground home module. JS constant Information define ("Css_url", Set_url. " public/home/css/");d efine (" Img_url ", Set_url." public/home/img/");d efine (" Js_url ", Set_url." public/home/js/");//define the css,img of the background admin module. JS constant Information define ("Admin_css_url", Set_url. " public/admin/css/");d efine (" Admin_img_url ", Set_url." public/admin/img/");d efine (" Admin_js_url ", Set_url." public/admin/js/");

We can see that the definition of constants also have a certain skill, some of the iconic things to be defined separately, such as website domain name, but also pay attention to the constant grouping design, so that reasonable and orderly, to improve the maintenance of code.

  • 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.