Php-mysql Course

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.

  - Syntax:
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.
  - Example (An Interface with two methods):
<?php
interface ITest {
  // List of methods
  public function Links();
  public function Tutorials($str, $nota);
}
?>

Implementing interface

Once you have defined an Interface, you can create classes that implement its methods.
A class can implement an interface using the implements keyword in its declaration.
  - Syntax:
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.
  - Example:
In this example we create a class (named WebDevelopment) which implements the ITest interface declared above.
<?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.
If we create another class that implements the ITest interface, this second class should contain the Links() and Tutorials() methods, otherwise will result in an error.
So, the Interface is useful when you want to create multiple classes whith the same structure of methods.

Interface as data type

The Interface can also be used as a data type for the parameter of a function. Then, that function can use that parameter as an object instance of the classes which implement that interface.
To understand better let's see the following example.
We create another class (called Languages) that implements the ITest interface:
<?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.
When this function is called, it accesses these methods defined in the object passed for the $obj parameter.
- By using this technique, you don't need to create the same function for every object instance.
This example will output:
coursesweb.net
php-mysql - 4
Good way
4 - marplo.net/php-mysql

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
PHP OOP - Interfaces

Last accessed pages

  1. PHP Script Website Mini-Traffic (7261)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142535)
  3. Zodiac Signs PHP code (7232)
  4. The Essene Gospel of Peace (2504)
  5. CSS3 Flexbox Container (1086)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (538)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)