Skip navigation

Q: How do I do an overloaded constructor in PHP?

A: You don’t need them

Consider the following

class Foo
{
  var $name ;
  function __construct()
  {
    # if the default ctor is invoked, name
    # this instance "unnamed"
    $this->name = 'unnamed' ;
  }

  # Fine and dandy, but what if
  # we want to supply a name
  # in an overloaded ctor?
  
  function __construct( $iname )
  {
    $this->name = $iname ; #doesn't work
    # because overloaded ctors of this style
    # aren't supported in PHP!!
  }
  
}

OK. Well I promised in the title of this post that you didn’t need overloaded constructors in php Here’s why.

Redefine class Foo as follows:


class Foo
{
  var $name ;

  # specify a single ctor, WITH DEFAULT VALUES
  # FOR PARAMETERS.  This is equivalent, and in my
  # opinion a better syntax
  # for getting overloaded constructors to work.
  function __construct( $iname = 'unnamed' )
  {
    $this->name = $iname ;
  }
}

There. Fewer lines of code, less mess, same effect. This was an excellent design decision by the php group.

Advertisements

2 Comments

    • stilgar
    • Posted July 24, 2009 at 2:18 am
    • Permalink

    Bullshit. The general idea of PHP is to let the coder decide how he codes, so why is this an excellent design decision? You’d always be free not to use it if they were to implement it.

    Your example isn’t very relevant, overloaded constructors and default arguments are not the same thing, and they are not incompatible. For instance, the following function signatures are not ambiguous.

    
    // dump current state, stack_trace etc...
    function debug();
    // dump object info
    function debug( $what, $show_children=false );
    

    Moreover, there are practical use-cases for overloaded constructors. Check ogogle too see how many developers miss them in PHP. An example:

    
    User::__construct( $login, $pass );
    User::__construct( $id, $login, $salt, $pass, $last_login );
    

    The former would be used to create a new User object, while the latter would be used by a database layer to create a User instance from query data. In other languages, the second declaration would typically be private but accessible to a factory class by declaring it as a friend. PHP forces us to use either more public members than necessary (breaking encapsulation), or to rely on hackish techniques such as __get/__set overloading and debug_trace exploitation. (all this works but is not pretty, and not pretty means harder to maintain, which means more time spent on earning less money, which means working on a crappier computer, which leads to RSI and eyestrain, and eventually premature death in horrible conditions, and many dead ponies and dangling pointers, the horror)

  1. You’d always be free not to use it if they were to implement it.

    A language enforces what its designers believe you should and should not be able to do. For example, operator overloading in Java is unsupported and that’s completely intentional.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: