Skip to content

Instantly share code, notes, and snippets.

@tbcorr
Last active September 24, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbcorr/26af5f405a5750ae3d54 to your computer and use it in GitHub Desktop.
Save tbcorr/26af5f405a5750ae3d54 to your computer and use it in GitHub Desktop.
Multiple Constructors
<?php
class MultipleConstructors {
private $prop_one; // string
private $prop_two; // int
private function __construct(){
$this->set_prop_one("");
$this->set_prop_two(0);
}
public static function from_array( array $properties ){
$instance = new MultipleConstructors();
$instance->set_prop_one( $properties['prop_one'] );
$instance->set_prop_two( $properties['prop_two'] );
return $instance;
}
public static function create( $prop_one, $prop_two ){
$instance = new MultipleConstructors();
$instance->set_prop_one( $prop_one );
$instance->set_prop_two( $prop_two );
return $instance;
}
public function get_prop_one(){
return $this->prop_one;
}
public function set_prop_one( string $prop ){
$this->prop_one = $prop;
}
public function get_prop_two(){
return $this->prop_two;
}
public function set_prop_two( int $prop ){
$this->prop_two = $prop;
}
}
$my_instance = MultipleConstructors::create( "Hello", 13 );
$my_other_instance = MultipleConstructors::from_array( array(
'prop_one' => "Hello",
'prop_two' => 13
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment