How to future-proof interface implementation?

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

What can I do, so that when a "stream()" method is added to "cameraInterface", it doesn't cause all of the existing modules implementing cameraInterface to break?

PHP Code: [ Select ]
interface cameraInterface
{
   /*
      $attributes is a URL encoded string of attributes for the camera
   */
   public function __construct($attributes);
 
   public function screenshot();
}
  1. interface cameraInterface
  2. {
  3.    /*
  4.       $attributes is a URL encoded string of attributes for the camera
  5.    */
  6.    public function __construct($attributes);
  7.  
  8.    public function screenshot();
  9. }


PHP Code: [ Select ]
final class camera
{
   private $module;
 
   public function screenshot()
   {
      return $this->module->screenshot();
   }
   
   public function __construct($class, $attributes)
   {
      include_once(ROOT_PATH . '/php/cameras/' . preg_replace('#[^a-z0-9_]+#', '', $class) . '.php');
 
      if(class_exists($class))
      {
         $this->module =  new $class($attributes);
 
         if($this->module instanceof cameraInterface)
         {
            return $this;
         }
         else
         {
            trigger_error("'{$class}' does not implement cameraInterface", E_USER_WARNING);
            return false;
         }
      }
 
      trigger_error("No module exists for camera of type '${class}'", E_USER_WARNING);
      return false;
   }
   
}
  1. final class camera
  2. {
  3.    private $module;
  4.  
  5.    public function screenshot()
  6.    {
  7.       return $this->module->screenshot();
  8.    }
  9.    
  10.    public function __construct($class, $attributes)
  11.    {
  12.       include_once(ROOT_PATH . '/php/cameras/' . preg_replace('#[^a-z0-9_]+#', '', $class) . '.php');
  13.  
  14.       if(class_exists($class))
  15.       {
  16.          $this->module =  new $class($attributes);
  17.  
  18.          if($this->module instanceof cameraInterface)
  19.          {
  20.             return $this;
  21.          }
  22.          else
  23.          {
  24.             trigger_error("'{$class}' does not implement cameraInterface", E_USER_WARNING);
  25.             return false;
  26.          }
  27.       }
  28.  
  29.       trigger_error("No module exists for camera of type '${class}'", E_USER_WARNING);
  30.       return false;
  31.    }
  32.    
  33. }


PHP Code: [ Select ]
class easycap implements cameraInterface
{
   protected $input;
 
   public function __construct($attributes)
   {
      parse_str($attributes, $attributes);
      foreach($attributes as $key => $val)
      {
         switch($key)
         {
            case 'input':
               $this->$key = $val;
            break;
         }
      }
   }
 
   public function screenshot()
   {
      $screenshot = "cache/cameras/screenshot-{$this->input}";
      $_screenshot   = ROOT_PATH . "/{$screenshot}";
 
      shell_exec('rm ' . "{$_screenshot}.*");
      shell_exec(ROOT_PATH . "/backup/somagic-easycap_1.1/somagic-capture -i {$this->input} -f 1 --vo={$_screenshot}.raw");
      shell_exec("convert -size 720x480 'UYVY:{$_screenshot}.raw' {$_screenshot}.jpg");
 
      return "{$screenshot}.jpg";
   }
}
  1. class easycap implements cameraInterface
  2. {
  3.    protected $input;
  4.  
  5.    public function __construct($attributes)
  6.    {
  7.       parse_str($attributes, $attributes);
  8.       foreach($attributes as $key => $val)
  9.       {
  10.          switch($key)
  11.          {
  12.             case 'input':
  13.                $this->$key = $val;
  14.             break;
  15.          }
  16.       }
  17.    }
  18.  
  19.    public function screenshot()
  20.    {
  21.       $screenshot = "cache/cameras/screenshot-{$this->input}";
  22.       $_screenshot   = ROOT_PATH . "/{$screenshot}";
  23.  
  24.       shell_exec('rm ' . "{$_screenshot}.*");
  25.       shell_exec(ROOT_PATH . "/backup/somagic-easycap_1.1/somagic-capture -i {$this->input} -f 1 --vo={$_screenshot}.raw");
  26.       shell_exec("convert -size 720x480 'UYVY:{$_screenshot}.raw' {$_screenshot}.jpg");
  27.  
  28.       return "{$screenshot}.jpg";
  29.    }
  30. }


PHP Code: [ Select ]
class foscam implements cameraInterface
{
   public $ip, $port;
   protected $user, $pwd;
   
   public function __construct($attributes)
   {
      parse_str($attributes, $attributes);
      foreach($attributes as $key => $val)
      {
         switch($key)
         {
            case 'ip':
            case 'port':
            case 'user':
            case 'pwd':
               $this->$key = $val;
            break;
         }
      }
   }
   
   public function screenshot()
   {
      return sprintf('http://%1$s:%2$s/snapshot.cgi?resolution=32&user=%3$s&pwd=%4$s',
         $this->ip,
         $this->port,
         $this->user,
         $this->pwd
      );
   }
}
  1. class foscam implements cameraInterface
  2. {
  3.    public $ip, $port;
  4.    protected $user, $pwd;
  5.    
  6.    public function __construct($attributes)
  7.    {
  8.       parse_str($attributes, $attributes);
  9.       foreach($attributes as $key => $val)
  10.       {
  11.          switch($key)
  12.          {
  13.             case 'ip':
  14.             case 'port':
  15.             case 'user':
  16.             case 'pwd':
  17.                $this->$key = $val;
  18.             break;
  19.          }
  20.       }
  21.    }
  22.    
  23.    public function screenshot()
  24.    {
  25.       return sprintf('http://%1$s:%2$s/snapshot.cgi?resolution=32&user=%3$s&pwd=%4$s',
  26.          $this->ip,
  27.          $this->port,
  28.          $this->user,
  29.          $this->pwd
  30.       );
  31.    }
  32. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I think the entire purpose of interface is to make sure everything that is using it is implementing all of those methods. So you would probably need to add that method to all of the classes first that are implementing that interface, even if its just an empty method.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I'm thinking I should have multiple interfaces for the modules to implement, such as cameraStillInterface, cameraStreamInterface, cameraControlInterface. Then when new methods are developed, modules can implement new interfaces optionally.

Then I can add to the camera class, and check for interface implementation in that camera class before trying to call it on the module.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 80 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.