any special meaning of MAIN() ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eko4v
Forum Newbie
Posts: 4
Joined: Thu Nov 04, 2010 7:25 am

any special meaning of MAIN() ?

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: any special meaning of MAIN() ?

Post by Eran »

No special meaning, and personally - I've never seen it being used, so I guess it's not that common.
eko4v
Forum Newbie
Posts: 4
Joined: Thu Nov 04, 2010 7:25 am

Re: any special meaning of MAIN() ?

Post 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. :?:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: any special meaning of MAIN() ?

Post 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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
eko4v
Forum Newbie
Posts: 4
Joined: Thu Nov 04, 2010 7:25 am

Re: any special meaning of MAIN() ?

Post 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()?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: any special meaning of MAIN() ?

Post 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
eko4v
Forum Newbie
Posts: 4
Joined: Thu Nov 04, 2010 7:25 am

Re: any special meaning of MAIN() ?

Post by eko4v »

Guys,
Thanks for your help to clear my confusion, I got it now.
echo
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: any special meaning of MAIN() ?

Post 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).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: any special meaning of MAIN() ?

Post by pickle »

Ah right - I knew it had some significance.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: any special meaning of MAIN() ?

Post by Weirdan »

pickle wrote:Ah right - I knew it had some significance.
Not in PHP.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: any special meaning of MAIN() ?

Post 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.
Post Reply