SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __construct called twice?

    Ok soo here is the deal. I have this class:

    PHP Code:
    class main {
      
      
    // this gets called twice.
      
    public function __construct() {
        echo 
    "1";
      }

    and I call it like this
    PHP Code:
    include("main.class.php");
    $main = new main(); 
    and i get the output: 11

    What is happening here? Why is my constructor getting called twice?

  2. #2
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    main is probably reserved by php and thus is called by php itself. try renaming it

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Tired that, and came out with the same thing. Any other ideas?

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I just tried it on my dev server and i got a single output...

    PHP Code:
     class main {
        
    // this gets called twice.
        
    public function __construct() {
            echo 
    "1";
        }
     }
     
     
    $main = new main(); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class main 
       
      
    // this gets called twice. 
      
    public function __construct() { 
        echo 
    "1"
      } 
    }


    $main = new main(); 
    Runs fine -- the result is 1. You may have some stray code in the main.class.php file that you are overlooking.
    intragenesis, llc professional web & graphic design

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's something missing here. The few lines of code you did post don't generate that output for me.

    I think you're leaving something out or... or I honestly don't know, actually.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh, logic_earth, we must had a universal mind meld -- I tested on my local server the same time you were. lol
    intragenesis, llc professional web & graphic design

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Indeed kinda scary...are you watching me?!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I just read all of your e-mail.
    intragenesis, llc professional web & graphic design


Bookmarks

Posting Permissions

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