Page 1 of 1

any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 7:40 am
by eko4v
I have seen some PHP source code, people often define a MAIN function inside a class, as I am new to write PHP but I know C/C++. I just wonder this MAIN has any special meaning in the class or not?
Though I did search the php manual and found that MAIN() shoud has nothing to do with the constructor, but just curious about the reason why I saw it often? It is just a coding custom or really something else?
Thanks for any help in advance,
echo

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 7:49 am
by Eran
No special meaning, and personally - I've never seen it being used, so I guess it's not that common.

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 7:54 am
by eko4v
Thank you for your reply. But I did seen some so I ask, like the source code of phpbb3 and some other open source package. :?:

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 10:16 am
by pickle
Probably a practice carried over from someone familiar with Java/C programming, where main() is the constructor of a class (I think).

As far as PHP is concerned, the main() function holds no special significance, and can just as easily be called stinkyBlueShoes().

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 11:15 am
by eko4v
Thanks pickle, thank you for your reply.
I also doubt the main() is used as the constructor or sort like, but I can not find anything to confirm this doubt. Under C++ the constructor and destructor would be called automatically by the instance of class, would the main() be called auto or not? If yes, can anyone help to explain how does it work?
Or it is just called by $foo->main() or $this->main()?

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 1:17 pm
by Weirdan
eko4v wrote: I also doubt the main() is used as the constructor or sort like, but I can not find anything to confirm this doubt. Under C++ the constructor and destructor would be called automatically by the instance of class, would the main() be called auto or not?
main is just a function / method name. It does not bear any specific mean as far as PHP concerned. In PHP constructor is called '__construct' and destructor is called '__destruct'
Or it is just called by $foo->main() or $this->main()?
Yes, that's right. Also it could be called ClassName::main() if it's defined as static

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 8:08 pm
by eko4v
Guys,
Thanks for your help to clear my confusion, I got it now.
echo

Re: any special meaning of MAIN() ?

Posted: Thu Nov 04, 2010 9:04 pm
by Jonah Bron
pickle wrote:Probably a practice carried over from someone familiar with Java/C programming, where main() is the constructor of a class (I think).
FYI, public static main() is the first function called when the program starts. The constructor is the same name as the class (in Java anyways).

Re: any special meaning of MAIN() ?

Posted: Fri Nov 05, 2010 11:15 am
by pickle
Ah right - I knew it had some significance.

Re: any special meaning of MAIN() ?

Posted: Fri Nov 05, 2010 11:38 am
by Weirdan
pickle wrote:Ah right - I knew it had some significance.
Not in PHP.

Re: any special meaning of MAIN() ?

Posted: Fri Nov 05, 2010 1:56 pm
by greyhoundcode
Perhaps slightly :offtopic: but ...
Jonah Bron wrote:FYI, public static main() is the first function called when the program starts. The constructor is the same name as the class (in Java anyways).
C# too. Prefer the PHP 5 approach of using __construct() and __destruct() though, which to my mind makes more sense.