Interface is a special class used like a template for a group of classes with similar functions, which must define a certain structure of methods.
Synthesized, Interface is a class with a list of methods which must be created in classes where it is implemented.
An interface is declared with the interface keyword. It can contain constants and method declarations, but not method bodies.
interface InterfaceName { const CONSTANT_NAME = value; public function methodName1(); public function methodName2(); ... }All methods declared within an interface must be public, and must be defined in the classes in which it is applied, having the same number of parameters indicated in Interface.
<?php interface ITest { // List of methods public function Links(); public function Tutorials($str, $nota); } ?>
class ClassName implements InterfaceName { // Class body }The classes which implement an interface must define all methods declared in it, with public attribute and the same number of parameters. They may contain other methods, too.
<?php // class that implements ITest interface class WebDevelopment implements ITest { // protected property protected $site = 'coursesweb.net'; // method required by interface public function Links() { return $this->site; } // method required by interface public function Tutorials($gen, $nota) { $re = $gen. ' - '. $nota; return $re; } // additional method public function setSite($site) { $this->site = $site; } } ?>This class defines the two methods required by the ITest interface ( Links() and Tutorials() ), a protected property ($site) and an accesor method ( setSite() ), that set the value of $site.
<?php class Languages implements ITest { // protected property protected $adr = 'marplo.net/'; // method required by interface public function Links() { return'Good way'; } // method required by interface public function Tutorials($gen, $nr) { $re = $nr.' - '. $this->adr. $gen; return $re; } } ?>Now we create a script that includes and uses the ITest interface and the two classes (WebDevelopment and Languages).
<?php // include the files with the code of the classes (if they are in external files): ITest, WebDevelopment and Languages include('interf.ITest.php'); // The Interface include('class.WebDevelopment.php'); include('class.Languages.php'); // function that accepts only arguments which are objects of the classes which implement ITest function courses(ITest $obj) { // calls the methods declared in ITest interface echo '<br />'. $obj->Links(); echo '<br />'. $obj->Tutorials('php-mysql', 4); } // creates the object instances $web_development = new WebDevelopment(); $limbi_straine = new Languages(); // calls the courses() function, passing the objects for argument courses($web_development); courses($limbi_straine); ?>(ITest $obj) makes the courses() function to accept only an argument that is an object of a class which implements ITest. Becouse we know that all these classes have defined the methods declared in the ITest interface ( Links() and Tutorials() ), we can call them within the function body.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net