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