By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

PHP OO Java Main Method equivalent???

P: n/a
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....

For example:

class myclass{
//my class's code
}

//Here is where I want to create a main method... or a function that
gets automatically called when the script is executed... that way this
"main" method would create instances of "myclass" and use them...
Cheers!

Jose

Jun 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jun 15, 6:57 am, pelon <pelonbiol...@gmail.comwrote:
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....

For example:

class myclass{
//my class's code

}

//Here is where I want to create a main method... or a function that
gets automatically called when the script is executed... that way this
"main" method would create instances of "myclass" and use them...

Cheers!

Jose

Have you tried a simple example?

Jun 15 '07 #2

P: n/a
fel
On Jun 15, 9:57 am, pelon <pelonbiol...@gmail.comwrote:
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....

<?php
class welcometophp {
function main(){
System.out.println("not that different from java, is it?");
}
}

welcometophp::main();
?>
>
For example:

class myclass{
//my class's code

}

//Here is where I want to create a main method... or a function that
gets automatically called when the script is executed... that way this
"main" method would create instances of "myclass" and use them...

Cheers!

Jose

Jun 15 '07 #3

P: n/a
On Jun 15, 9:57 am, pelon <pelonbiol...@gmail.comwrote:
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....

For example:

class myclass{
//my class's code

}

//Here is where I want to create a main method... or a function that
gets automatically called when the script is executed... that way this
"main" method would create instances of "myclass" and use them...
You can't get away without something outside, but you can do as the
person above me pointed out: make a static function and call it first
thing.
>
Cheers!

Jose

Jun 15 '07 #4

P: n/a
C.
On 15 Jun, 15:57, pelon <pelonbiol...@gmail.comwrote:
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....
First thing is the PHP, unlike Java has first class functions. Second
thing, is that PHP, like most scripting languages has an implicit
entry point at the beginning of the invoked file so all you need to do
is this:

BOF>>>>>>>>>>>
<?php

$obj = new myclass();

class myclass{
//my class's code
}

?>
<<<<<<<<<<<EOF

Although its generally considered neater to put class definitions in
their own include file so...

BOF>>>>>>>>>>>
<?php

require_once('myclass.inc.php');
$obj = new myclass();

?>
<<<<<<<<<<<EOF

HTH

C.

Jun 15 '07 #5

P: n/a
Have you read this?
http://www.php.net/manual/en/language.oop5.basic.php
pelon wrote:
Hey!

I'm new to OO in PHP but somewhat familiar with this paridigm in
JAVA....

So I was wondering if it is possible to have within a PHP script
something equivalent to a main method as it is possible in JAVA....

For example:

class myclass{
//my class's code
}

//Here is where I want to create a main method... or a function that
gets automatically called when the script is executed... that way this
"main" method would create instances of "myclass" and use them...
Cheers!

Jose
Jun 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.