ThinkPHP user-defined constants and system constants usage _ PHP Tutorial

Source: Internet
Author: User
Tags constant definition website domain names
Use of custom constants and system constants of ThinkPHP. We all know that ThinkPHP's custom constants and the constants used by system constants define a constant volume. in php, the constant definition rule is define (constant name, "constant "); that is, the use of custom constants and system constants of ThinkPHP

Constants are defined as a constant volume. in php, the constant definition rule is define ("constant name", "constant value ");

That is, it is meaningless to use this constant to define several constants on all pages. it is important to use the constant area to optimize the code, optimization code is to improve the maintainability of the code. sometimes it also simplifies the code, but this is definitely not the main purpose.

In traditional code writing, we adopt direct writing, such as url addresses. although these things can be directly implemented, in the later maintenance process of the program, it is very difficult first. although the replacement function can be used, this is not a long-term plan, and there is a better way than this, it is to define constants for use. in this way, in later maintenance, if you change the drive letter or variable information, you can directly modify the variable. Although the principle is simple, however, it is not easy to use it flexibly.

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

// Echo"

";
// var_dump(get_defined_constants());
// echo "
";

You will be stuck in a bunch of things, which are provided for you to use by all the system constants, but it is worth noting that whether it is a custom constant or a system constant, the most common ones are in the back. pay attention to this point:

The following is an example code for defining constants. The think template is used as an example.

// Define the constant define ("SET_URL", "http://shop.com/"); // define the css, img of the front-end HOME module. Js constant information define ("CSS_URL", SET_URL. "Public/Home/css/"); define ("IMG_URL", SET_URL. "Public/Home/img/"); define ("JS_URL", SET_URL. "Public/Home/js/"); // defines the css and img of the Admin module in the background. Define ("ADMIN_CSS_URL", SET_URL. "Public/Admin/css/"); define ("ADMIN_IMG_URL", SET_URL. "Public/Admin/img/"); define ("ADMIN_JS_URL", SET_URL. "Public/Admin/js /");

We can see that defining constants also requires some skills. some iconic things should be defined separately, such as website domain names. pay attention to the group design of constants to ensure proper order, to improve code maintainability.

Constant constants are defined as a constant volume. in php, the constant definition rule is define ("constant name", "constant"), that is...

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.