SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb OOP constructor - just a quick question

    I have a class that doesn't need a constructor. Is it OK to have no constructor? e.g.
    PHP Code:
     class barry 
     
        function 
    freddy () {
            echo 
    'freddy';
            }
     
        } 
    see no constructor. Is this good or bad programming practice?

    Thanks
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  2. #2
    SitePoint Guru
    Join Date
    May 2004
    Location
    santa rosa, ca
    Posts
    969
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's nothing wrong with no contructor in a case like that.
    nondenominational, noncommercial, nonprofit,
    listener-supported, 24-hour, Christian ministry:
    Listen Live Online

  3. #3
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  4. #4
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Technically, you may not need a constructor here, because this class has no members that need to be initialized, but I think it's at least good practice to always include a constructor, even if it's empty.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I think it's at least good practice to always include a constructor, even if it's empty.
    So you think adding code that just sits there and does nothing is good practice?

  6. #6
    SitePoint Member
    Join Date
    Feb 2004
    Location
    New York
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Standard practice ,you should have it there.
    I believe it has to do more with the compiler
    ________________________________
    SunsetHost.Com
    Complete Internet Presence Providers
    Imagine.Create.Upload://Today

  7. #7
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mkrz
    Technically, you may not need a constructor here, because this class has no members that need to be initialized, but I think it's at least good practice to always include a constructor, even if it's empty.
    Teh, what about a class that only has static methods ?

  8. #8
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Standard practice..
    Just because something is standard practice does not mean that it's good. Just look at SQL

    I believe it has to do more with the compiler
    I've never gotten any compilation errors when I did not have a constructor

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just look at SQL
    what's ever wrong with SQL ? it's beautiful.

    well ... a reason for allways supplying a constructor is, that if you should need one at a later point, descendands wouldn't have to be updated. (at least in php4). consider this :

    PHP Code:
    class foo {
    }
    class 
    bar extends foo {
        function 
    bar()
        {
            
    // do stuff
        
    }

    now you change the base class (foo) into :
    PHP Code:
    class foo {
        function 
    foo()
        {
            
    // do stuff
        
    }

    witch will force you to also update the derived class (bar)
    PHP Code:
    class bar extends foo {
        function 
    bar()
        {
            
    $this->foo();
            
    // do stuff
        
    }

    had you supplied a default (empty) constructor from the beginning, you could have called this from bar in the first place.

    Then again (just to contradict my own post) you may argue that any change to the baseclass' constructor has impact on the derived classes.

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    if you should need one at a later point, descendents wouldn't have to be updated.
    ...snip...
    Then again (just to contradict my own post) you may argue that any change to the baseclass' constructor has impact on the derived classes.
    Surely this is not the case if the base class's API does not change and the derived classes use that API (e.g. using setters and getters rather than accessing base class vars directly)?


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
  •