The Cannotredeclare method is defined as follows:
Class maaper {
......
Public function getProperties (){
Function getName ($ reflectionProperties ){
Return $ reflectionProperties-> name;
}
$ Domain = $ this-> get_domain ();
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
$ Properties = array_map ('getname', $ reflectionProperties );
Return $ properties;
}
......
}
The calling process is as follows (note: $ mapper_1 and $ mapper_2 call methods at the same runtime ):
$ Mapper_1 = new mapper ();
$ Mapper_2 = new mapper ();
Var_dump ($ mapper_1-> getProperties (); // The Returned result is correct.
Var_dump ($ mapper_2-> getProperties (); // error Cannot redeclare getName ()
As follows:
Reply to discussion (solution)
Do you not read the error message?
Fatal error: Cannot redeclare getName ()
Fatal error: unable to re-declare getName ()
Class maaper {
Protected function getName ($ reflectionProperties ){
Return $ reflectionProperties-> name;
}
Public function getProperties (){
$ Domain = $ this-> get_domain ();
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
$ Properties = array_map (array ($ this, 'getname'), $ reflectionProperties );
Return $ properties;
}
You can also write
Class maaper {
Public function getProperties (){
$ GetName = function ($ reflectionProperties ){
Return $ reflectionProperties-> name;
};
$ Domain = $ this-> get_domain ();
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
$ Properties = array_map ($ getName, $ reflectionProperties );
Return $ properties;
}
Do you not read the error message?
Fatal error: Cannot redeclare getName ()
Fatal error: unable to re-declare getName ()
Class maaper {
Protected function getName ($ reflectionProperties ){
Return $ reflectionProperties-> name;
}
Public function getProperties (){
$ Domain = $ this-> get_domain ();
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
$ Properties = array_map (array ($ this, 'getname'), $ reflectionProperties );
Return $ properties;
}
You can also write
Class maaper {
Public function getProperties (){
$ GetName = function ($ reflectionProperties ){
Return $ reflectionProperties-> name;
};
$ Domain = $ this-> get_domain ();
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
$ Properties = array_map ($ getName, $ reflectionProperties );
Return $ properties;
}
Thank you very much.
But why is there a re-declaration error?
You have
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
If you re-enter getProperties, will the functions defined in getProperties be defined again?
You have
$ ReflectionProperties = $ domain-> getProperties (ReflectionProperty: IS_PUBLIC );
If you re-enter getProperties, will the functions defined in getProperties be defined again?
Although $ domain-> getProperties are called twice, the getProperties method is defined in the class to which the $ domain object belongs. the getName method is not used. The error message indicates that the getName method located in getProperties in class mapper cannot be re-declared.
So .... That's not the case.
Function getName ($ reflectionProperties ){
Return $ reflectionProperties-> name;
}
Global functions are defined wherever they are.
Function getName ($ reflectionProperties ){
Return $ reflectionProperties-> name;
}
Global functions are defined wherever they are.
In Chinese
Thank you !!!