- Class A {
- Public Function __tostring () {
- return ' bar ';
- }
- }
- $a = new A ();
- Define (' foo ', $a);
- echo foo;
- Output bar
Copy CodeHow the Define in PHP is implemented:
Zend_function (define)
- {
- Char *name;
- int Name_len;
- Zval *val;
- Zval *val_free = NULL;
- Zend_bool non_cs = 0;
- int case_sensitive = Const_cs;
- Zend_constant C;
Receive 3 parameters, String,zval,bool
- if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Sz|b", &name, &name_len, &val, &non_cs) = = FAILURE) {
- Return
- }
is case sensitive
- if (Non_cs) {
- case_sensitive = 0;
- }
If the Define class constant, the error
- if (ZEND_MEMNSTR (Name, "::", sizeof ("::")-1, name + Name_len)) {
- Zend_error (e_warning, "Class constants cannot be defined or redefined");
- Return_false;
- }
Get the real value, save it with Val
- Repeat
- Switch (Z_type_p (val)) {
- Case Is_long:
- Case Is_double:
- Case is_string:
- Case Is_bool:
- Case Is_resource:
- Case Is_null:
- Break
- Case Is_object:
- if (!val_free) {
- if (Z_obj_ht_p (val)->get) {
- Val_free = val = z_obj_ht_p (val)->get (Val tsrmls_cc);
- Goto repeat;
- } else if (Z_obj_ht_p (val)->cast_object) {
- Alloc_init_zval (Val_free);
- if (Z_obj_ht_p (Val)->cast_object (Val, Val_free, is_string tsrmls_cc) = = SUCCESS) {
- val = val_free;
- Break
- }
- }
- }
- /* No Break */
- Default
- Zend_error (e_warning, "Constants may be evaluate to scalar values");
- if (Val_free) {
- Zval_ptr_dtor (&val_free);
- }
- Return_false;
- }
- Building constants
- C.value = *val;
- Zval_copy_ctor (&c.value);
- if (Val_free) {
- Zval_ptr_dtor (&val_free);
- }
- C.flags = case_sensitive; /* Non Persistent *///If case is not sensitive, then 0, sensitivity is 1
- C.name = zend_strndup (name, Name_len);
- C.name_len = name_len+1;
- C.module_number = php_user_constant; Label non-kernel constants, but user-defined constants
- Register constants
- if (Zend_register_constant (&c tsrmls_cc) = = SUCCESS) {
- Return_true;
- } else {
- Return_false;
- }
- }
Copy CodeNote that a loop that starts with repeat also uses a goto statement t_t The purpose of this code is: for Int,float,string,bool,resource,null, the actual defined constants are used directly for object, you need to convert the object to one of the above 6 types (if it is still object after transformation, Continue to transform) how do I make an object into one of 6 types? There are 2 ways to look at the code:
- if (Z_obj_ht_p (val)->get) {
- Val_free = val = z_obj_ht_p (val)->get (Val tsrmls_cc);
- Goto repeat;
- }
- The __tostring () method is called in the Cast_object
- else if (Z_obj_ht_p (val)->cast_object) {
- Alloc_init_zval (Val_free);
- if (Z_obj_ht_p (Val)->cast_object (Val, Val_free, is_string tsrmls_cc) = = SUCCESS)
- {
- val = val_free;
- Break
- }
- }
Copy Code1,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 located in PHP-SRC/ZEND/ZEND-OBJECT-HANDLERS.C:
Zend_api int zend_std_cast_object_tostring (zval *readobj, zval *writeobj, int type TSRMLS_DC)/* {{* * *
- {
- Zval *retval;
- Zend_class_entry *ce;
Switch (type) {
- Case is_string:
- CE = z_objce_p (readobj);
- If __tostring is defined in the user's class, an attempt is made to invoke the
- if (ce->__tostring &&
- (Zend_call_method_with_0_params (&readobj, CE, &ce->__tostring, "__tostring", &retval) | | EG (Exception))) {
- ......
- }
- return FAILURE;
- ......
- }
- return FAILURE;
- }
Copy CodeFrom the above specific implementation, the default cast_object is to find the class __tostring method and then call ... Back to the first example, define (' foo ', $a), since $ A is an instance of a and __tostring is defined in Class A, the Foo constant is actually equal to the return value of ToString bar. PS: Keep digging a little bit of detail. 1,define has a return value that is usually written directly in our definition constants: Define (' foo ', 123); However, from the implementation of define, it has a return value. According to the description in the manual: Returns TRUE on success, or FALSE on failure. Under what circumstances will define fail? As an example:
Define (' Php_int_max ', 1); Returns false
Define (' FOO ', 1); Returns True
- Define (' FOO ', 2); Returns false
Copy CodeThe 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 again review the implementation of define, which only determines if name is xxx::yyy this form. In other words, define almost no requirement for its name, and of course it does not require that name be a valid PHP variable name. Therefore, we can let define's constants take some strange names. For example:
- Define (' >_< ', 123); Returns True
- Echo >_<; Syntax error
Copy CodeHowever, 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:
- Define (' >_< ', 123); Returns True
- ECHO constant (' >_< '); Output 123
Copy Code |