PHP object interface

Object Interface (interface)

Using the interface (interface), which can specify a class must implement method, but need not define the specific content of these methods.

Interface by  interface  keyword to define, like the definition of a standard of class, but the definition of all the methods are empty.

All methods defined in the interface must be public, which is characteristic of the interface.

 

Implement ( the implements )

To implement an interface, using  implements  operator. Class all the methods defined in the interface must be implemented, otherwise it will report a fatal error. Class can implement multiple interfaces, with a comma-separated names of the plurality of interfaces.

Note:

When implementing a plurality of interfaces, the method can not have the same name interface.

Note:

Interfaces can inherit, by using  extends  operator.

Note:

Class to implement an interface, and the interface must be used in the method as defined in exactly the same manner. Otherwise it will cause a fatal error.    

Examples

? < PHP 

// declare a 'iTemplate' interfaces 
interface ITemplate 
{ 
    public function setVariable (name $, $ var );
     public function the getHtml ($ Template); 
} 


// implement the interface
 // following this is the correct 
class Template ITemplate the implements 
{ 
    Private $ VARS = Array (); 
  
    public function setVariable (name $, $ var ) 
    { 
        $ the this -> VARS [$ name] = $ var ; 
    } 
  
    public function the getHtml ($ Template) 
    { 
        the foreach($this->vars as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }
 
        return $template;
    }
}

 

Guess you like

Origin www.cnblogs.com/ryanzheng/p/11404710.html