Jump to content

How to prevent error when Class is called minus certain parameters?


Recommended Posts

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.

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.