Integralist Posted February 17, 2009 Share Posted February 17, 2009 Hello, I have the following Class: class ClassGallery { // Private Properties private $directory; private $imagesPerPage = 10; public function __construct($dir, $count) { if (empty($dir)) { exit('Sorry, no directory value was provided and this is a required parameter'); } $this->directory = $dir; $this->imagesPerPage = $count; } public function display() { echo($this->directory . ' / ' . $this->imagesPerPage); } } ?> and I'm calling it like so: <?php include('Class.Gallery.php'); $gallery = new ClassGallery('images', 20); $gallery->display(); ?> But if I create a new instance without any parameters, e.g. $gallery = new ClassGallery(); then I get the following error message? Warning: Missing argument 1 for ClassGallery::__construct(), called in C:\Inetpub\wwwroot\PHP\Gallery\index.php on line 15 and defined in C:\Inetpub\wwwroot\PHP\Gallery\Class.Gallery.php on line 20 Warning: Missing argument 2 for ClassGallery::__construct(), called in C:\Inetpub\wwwroot\PHP\Gallery\index.php on line 15 and defined in C:\Inetpub\wwwroot\PHP\Gallery\Class.Gallery.php on line 20 Sorry, no directory value was provided and this is a required parameter I had hoped that my conditional statement in the Class itself would have prevented the error from showing (and it seems to be called if you see the last line of the error is actually the exit() method from my Class conditional statement). But I'm not sure how to prevent the other error, other than putting an @ at the start of the instantiation line... e.g. @ $gallery = new ClassGallery(); but that wouldn't be a good solution if this Class was made into a public API as you would thus be relying on the developer to make sure they instantiated the class using @ preceding it? Any help appreciated. Kind regards, M. Quote Link to comment https://forums.phpfreaks.com/topic/145581-how-to-prevent-error-when-class-is-called-minus-certain-parameters/ Share on other sites More sharing options...
JonnoTheDev Posted February 17, 2009 Share Posted February 17, 2009 By specifying default parameter values in the method __construct($dir = false, $count = false) Then $x = new ClassGallery() will work Quote Link to comment https://forums.phpfreaks.com/topic/145581-how-to-prevent-error-when-class-is-called-minus-certain-parameters/#findComment-764301 Share on other sites More sharing options...
Zane Posted February 17, 2009 Share Posted February 17, 2009 of course if you do that __construct($dir = false, $count = false) then you'll have to change your function around accordingly...or you'll get a totally different error for when you try to access a directory. something like.... error accessing directory because it is false I would set a small line like if($dir === false) $this->directory = "your/directory/path"; within your function Quote Link to comment https://forums.phpfreaks.com/topic/145581-how-to-prevent-error-when-class-is-called-minus-certain-parameters/#findComment-764310 Share on other sites More sharing options...
Integralist Posted February 17, 2009 Author Share Posted February 17, 2009 aha! thank you! Quote Link to comment https://forums.phpfreaks.com/topic/145581-how-to-prevent-error-when-class-is-called-minus-certain-parameters/#findComment-764312 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 Personally I'd go with __construct($dir = NULL, $count = NULL) So I can check the arguments with isset Quote Link to comment https://forums.phpfreaks.com/topic/145581-how-to-prevent-error-when-class-is-called-minus-certain-parameters/#findComment-764315 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.