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 Link to comment https://forums.phpfreaks.com/topic/214833-handle-null-argument/ Share on other sites More sharing options...
AbraCadaver Posted September 30, 2010 Share Posted September 30, 2010 public function __construct($t=null){ Link to comment https://forums.phpfreaks.com/topic/214833-handle-null-argument/#findComment-1117592 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. Link to comment https://forums.phpfreaks.com/topic/214833-handle-null-argument/#findComment-1117598 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 Link to comment https://forums.phpfreaks.com/topic/214833-handle-null-argument/#findComment-1117616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.