Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is there a PHP equivalent of "main" in Java?

    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.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I think you're looking for the __construct method:
    PHP Code:
    class foo
    {
        public function 
    __construct($params null)
        {
            echo 
    'asdf';
        }
    }
    $foo = new foo(); 
    edit: manual on "Magic Methods": http://theserverpages.com/php/manual...oop5.magic.php
    Last edited by ralph l mayo; 04-24-2006 at 07:19 PM.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.
    Code:
    class Test {
      public Test() {
        //constructor
      }
      public static void main() {
        //do stuff
      }
    }
    PHP Code:
    <?php
    class Test {
      function 
    __construct() {
        
    //construct
      
    }

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

  • #4
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Oops, yeah, I mixed up 'called' and 'instantiated' in my head reading your question. Sorry 'bout that.

  • #6
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No problem.

  • #7
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •