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.