4

Is there a way of using the __construct function in PHP to create more than one constructor in a hierarchical pattern.

For example, I want to create a new instance of my Request class using the constructor

__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );

But I would like a convenience constructor like this:

__construct( $url );

…whereby I can send a URL and the properties are extracted from it. I then call the first constructor sending it the properties I extracted from the URL.

I guess my implementation would look something like this:

function __construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments )
{
    //
    //  Set all properties
    //

    $this->rest_noun = $rest_noun;
    $this->rest_verb = $rest_verb;
    $this->object_identifier = $object_identifier;
    $this->additional_arguments = $additional_arguments;
}

function __construct( $url )
{
    //
    //  Extract each property from the $url variable.
    //

    $rest_noun = "component from $url";
    $rest_verb = "another component from $url";
    $object_identifier = "diff component from $url";
    $additional_arguments = "remaining components from $url";

    //
    //  Construct a Request based on the extracted components.
    //

    this::__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );
}

…but I’m quite a beginner in PHP so wanted to get your advice on the topic to see if it would work or even if there’s a better way to do it.

My guess is if it comes down to it I can always just use a static function for my convenience.

Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • 1
    Possible duplicate of [Best way to do multiple constructors in PHP](http://stackoverflow.com/questions/1699796/best-way-to-do-multiple-constructors-in-php) – Kevin Oct 21 '15 at 23:32
  • Just create a constructor helper `ConstructFoo::fromUrl($url);` – zerkms Oct 21 '15 at 23:34

3 Answers3

3

Just extends your Request class:

class RequestWithAnotherContructor extends Request
{
        function __construct($url) {
            $rest_noun = "component from $url";
            $rest_verb = "another component from $url";
            $object_identifier = "diff component from $url";
            $additional_arguments = "remaining components from $url";

            // call the parent constructors
            parent::__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );
       }
}
Federkun
  • 36,084
  • 8
  • 78
  • 90
1

You could do something with func_get_args as noted in Best way to do multiple constructors in PHP

function __construct($param) {
    $params = func_get_args();
    if (count($params)==1) {
        // do first constructor
    } else {
        // do second constructor
    }
}
Community
  • 1
  • 1
Kevin
  • 1,666
  • 9
  • 10
0

why dont you call the function from the constructor?

function __construct( $url ){
   //stuff you need
   $this->do_first($stuff)
}

function do_first($stuff){
   //stuff is done
}
Dan
  • 3,755
  • 4
  • 27
  • 38
  • Cos that would be technically wrong. The designated constructor is the one with all the properties needed for the construction of the object, and the convenience constructor would not pass values that could potentially be calculated or defaulted. – Adam Carter Oct 21 '15 at 23:29