Errors returned when array_map is used in class methods --- Cannotredeclare

Source: Internet
Author: User
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 !!!

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.