Re: What is the practical use of "abstract" and "interface"?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2008-04-16 at 12:20 +0100, Tony Marston wrote:
> The term "abstract" has been adequately defined in this thread, so I won't
> repeat it.
> 
> However, there is one important aspect of the term "interface" which I think
> that most people seem to miss - it is not necessary to use the term
> "interface" in order to have an interface. Let me explain with a code
> sample:
> 
> class foo {
>     function something ($arg1, $arg2, ...) {
>         ....
>     } // end something
> } // end class foo
> 
> Here I have defined a class "foo" with a method called "something". This
> method as it stands is already an interface (as in Application Program
> Interface or API) and does not require anything extra for it to be accessed.
> All I need is the following:
> 
>     $foo = new foo.
>     $result = $foo->something($arg1, $arg2, ...);
> 
> In order to include the term "interface" in the code you need something like
> the following (which is taken from the PHP manual):
> 
> // Declare the interface 'iTemplate'
> interface iTemplate
> {
>     public function setVariable($name, $var);
>     public function getHtml($template);
> }
> 
> // Implement the interface
> // This will work
> class Template implements iTemplate
> {
>     private $vars = array();
> 
>     public function setVariable($name, $var)
>     {
>         $this->vars[$name] = $var;
>     }
> 
>     public function getHtml($template)
>     {
>         foreach($this->vars as $name => $value) {
>             $template = str_replace('{' . $name . '}', $value, $template);
>         }
> 
>         return $template;
>     }
> }
> 
> All that extra code for absolutely no benefit! It is possible to define an
> interface (as in API) without actually using the term "interface", so IMHO
> the term "interface" is totally redundant and a waste of time.

While I agree that Interfaces are mostly a lot of extra code, I have to
also say that they are there primarily to enforce a contract between the
user of the interface and their classes that claim to implement the
interface. If someone creates a class that "Implements" an interface,
then when I have to go edit or use the class, it had better damn well
implement what it says it does :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux