Read the manual that define defines constants that allow only:
Only scalar and null are allowed. The type of scalar is integer, Float,string, or Boolean. It is also possible to define a constant value of type resource, but it is not recommended to do so, which can lead to the occurrence of an unknown condition.
Read the PHP source code today, found that the second parameter of define can actually be an object.
Put a sample first:
< span="">< span="">< span="">< span="">< span="">< span=""> < span=""> < span=""> ' bar ' < span=""> < span=""> < span=""> < span=""> foo < span="">; < span="">
Output bar
Then let's look at how the Define in PHP is implemented:
***val_free ==Case_sensitive = (Zend_parse_parameters (Zend_num_args () TSRMLS_CC,, &name, &name_len, &val, &non_cs) = = = (Zend_memnstr (Name,,() -, name +(!(Z_obj_ht_p (Val)= val = Z_obj_ht_p (val) (Z_obj_ht_p (Val)(Z_obj_ht_p (Val)->cast_object (Val, Val_free, is_string tsrmls_cc) = == &C.value = *&&= Case_sensitive;== name_len+=< span=""> (Zend_register_constant (&c tsrmls_cc) = =Note that a loop that starts with repeat also uses a goto statement t_t
The function of this code is:
- For Int,float,string,bool,resource,null, these values are used directly when the constants are actually defined
- For object, you need to convert the object to one of the above 6 types (if it is still object after transformation, continue with the transformation)
How do I put an object into one of 6 types? There are 2 ways to look at the code:
< span="">(z_obj_ht_p< span="">< span="">(val) = val = Z_obj_ht_p (val),< span="">< span="">< span="">
The __tostring () method is called < span=""> < span=""> in Cast_object (Z_obj_ht_p (val) < span=""> (Z_obj_ht_p (Val)->cast_object (Val, Val_free, is_string tsrmls_cc) < span=""> = = < span="">=< span="">< span="">< span="">
1,z_obj_ht_p (Val)->get, after the macro is expanded (*val). Value.obj.handlers->get
2,z_obj_ht_p (Val)->cast_object, after the macro is expanded (*val). Value.obj.handlers->cast_object
Handlers is a struct with many function pointers, as defined in _zend_object_handlers. The function pointers in the struct are used to manipulate objects, such as reading/modifying object properties, getting/calling object methods, and so on ... Get and Cast_object are also one of them.
For generic objects, PHP provides the standard Cast_object function zend_std_cast_object_tostring, which is in PHP-SRC/ZEND/ZEND-OBJECT-HANDLERS.C:
ZEND_API zend_std_cast_object_tostring (zval *readobj, Zval *writeobj, type TSRMLS_DC) *= (ce->__tostring &&< Span (Zend_call_method_with_0_params (span>&readobj, CE, &ce->__tostring, , &retval) | | < Span return< span>
From the above specific implementation, the default cast_object is to find the class __tostring method and then call ...
Back to the first example,< span="">(' foo ',< span="">< span="">
< span="">
< span="">
< span="">
< span="">
< span="">
< span="">(' Php_int_max ', 1); < span=""> ( < span="">' FOO ', 1); < span=""> < span="">< span="">< span="">< span="">< span="">
The above code contains two cases where we try to redefine the predefined constants of the PHP kernel, such as Php_int_max, which will obviously fail. The second scenario is that we've defined a constant foo somewhere in the code, and then we define it again in the next program, which can also lead to failure. Therefore, it is best to write all constants that need to be defined when coding, so as not to cause name duplication.
2, constant name No limit
< span="">
< span="">There is no requirement for its name, and of course No name is a valid PHP variable name. Therefore, we can let define's constants take some strange names. For example:
< span="">(' >_< ', 123); >_<; < span=""> < span=""> < span=""> < span="">< span="">
However, if such constants are defined, they cannot be used directly and will be reported as grammatical errors. The correct method of use is as follows:
< span="">(' >_< ', 123); < span=""> < span=""> ( ' >_< ' < span=""> ); < span=""> < span="">< span="">
http://www.bkjia.com/PHPjc/440356.html www.bkjia.com true http://www.bkjia.com/PHPjc/440356.html techarticle the manual says that the constants defined by define are allowed only: scalar and null are allowed only. The type of scalar is integer, Float,string, or Boolean. You can also define a constant value of type resource ...