TomTees Posted September 30, 2010 Share Posted September 30, 2010 How do I make my constructor work when I don't pass in an argument. That is, it should efault to null. class Something{ private $type=null; public function __construct($t){ $this->type = $t; } public function setSomething($t){ $this->type = $t; } public function getSomething(){ if (is_null($this->type)) { return "None for you!"; } else { return "Something is " . $this->type . "."; } } } TomTees Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 30, 2010 Share Posted September 30, 2010 public function __construct($t=null){ Quote Link to comment Share on other sites More sharing options...
the182guy Posted September 30, 2010 Share Posted September 30, 2010 If you're looking to make it more advanced you can achieve constructor overloading with PHP using func_get_args() and func_num_args(). Unfortunately it's not natively implemented. Quote Link to comment Share on other sites More sharing options...
TomTees Posted September 30, 2010 Author Share Posted September 30, 2010 public function __construct($t=null){ I thought I tried that earlier, but it didn't work. Oh well. Thanks!!!! TomTees Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.