Interface can be created through interface keyword which is followed by interface name. Methods are not defined in interfaces. The class which we implement must defined all the methods of an interface. All the methods which are declared in interface should be public. PHP class can implement multiple interfaces at a time.
interface Cache{
public function setKey($key,$value);
public function getKey($key);
}
To use interface in a class, implements keyword is used.
class productCache implements Cache {
public $productData = array();
public function setKey($key,$value){
$this->productData[$key] = $value;
}
public function getKey($key){
return $this->productData[$key];
}
}
The use of Interfaces, Abstract Classes, Traits in program is not mandatory but it allows you to manage code and keep it well structured. As a PHP Developer, this would be very much helpful to you.
Let's create cache interface which has two methods such as setKey and getKey. Any class which implement Cache interface, declare setKey and getKey method.
interface Cache{
public function setKey($key,$value);
public function getKey($key);
}
To use interface in a class, implements keyword is used.
class productCache implements Cache {
public $productData = array();
public function setKey($key,$value){
$this->productData[$key] = $value;
}
public function getKey($key){
return $this->productData[$key];
}
}
The use of Interfaces, Abstract Classes, Traits in program is not mandatory but it allows you to manage code and keep it well structured. As a PHP Developer, this would be very much helpful to you.
0 comments:
Post a Comment