...

View Full Version : Is there a PHP equivalent of "main" in Java?



ibnpaul
04-24-2006, 08:07 PM
Hi all,

I am a coder trained in Java working on my first PHP project. I am trying to keep everything OO, but I am having trouble tracking down the "main" function in PHP. In Java, "main" is the function that runs by default when an object is called. If there is not a corresponding function name in PHP, is there any agreed upon way of handling this? Thanks a lot.

ralph l mayo
04-24-2006, 08:13 PM
I think you're looking for the __construct method:


class foo
{
public function __construct($params = null)
{
echo 'asdf';
}
}
$foo = new foo();


edit: manual on "Magic Methods": http://theserverpages.com/php/manual/en/language.oop5.magic.php

GJay
04-24-2006, 08:14 PM
there isn't really an equivalent, running a php script will start at the top and execute commands it finds as it works its way down ('ignoring' classes and function until they are called).

__construct gets called when an object is instantiated if that helps.


class Test {
public Test() {
//constructor
}
public static void main() {
//do stuff
}
}



<?php
class Test {
function __construct() {
//construct
}

function init() {
//do stuff
}
}
$test=new Test;
$test->init();
?>


maybe?

ibnpaul
04-24-2006, 08:22 PM
ralph | mayo: Thanks, but there is a construct method in Java as well. The main function is a function that is run automatically when an already initialized object is called with no function name, i.e. a default function.

GJay: Thanks. This was what I had thought, that there is no equivalent. I was thinking about doing something similar to the code you wrote, but I wanted to see if I could save some logic and space :).

Thanks a lot to both of you for the very quick replies, my question is answered.

ralph l mayo
04-24-2006, 08:25 PM
Oops, yeah, I mixed up 'called' and 'instantiated' in my head reading your question. Sorry 'bout that.

ibnpaul
04-24-2006, 08:36 PM
No problem.

trib4lmaniac
04-24-2006, 09:47 PM
I'm 100% sure you've already got it. But you'd have to call a function called main from the constructor manually. Kind of obvious though :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum