The actual use of PHP interface classes versus abstract classes

  • 2020-03-31 17:10:35
  • OfStack

1. PHP interface class :interface
Their role is very simple, actually when there are a lot of people together to develop a project, may go to call others to write some of the classes, then you will ask, how do I know he's a function of the method is how named, PHP interface class to play a role, this time when we define an interface class, it is the following subclasses must implement, such as:
 
interface Shop 
{ 
public function buy($gid); 
public function sell($gid); 
public function view($gid); 
} 

If I declare a shop interface class that defines three methods: buy, sell, and view, then all subclasses that inherit this class must implement none of the three methods. In fact, the interface class is simply the template of a class and the rules of a class. If you belong to this class, you must follow my rules. No one can do without one.
 
class BaseShop implements Shop 
{ 
public function buy($gid) 
{ 
echo(' You have purchased ID for  :'.$gid.' The goods '); 
} 
public function sell($gid) 
{ 
echo(' You sell ID for  :'.$gid.' The goods '); 
} 
public function view($gid) 
{ 
echo(' You looked at the ID for  :'.$gid.' The goods '); 
} 
} 

Think about how convenient it is to have an interface class in a large collaborative project, so you don't have to ask people what the method name of your x function is, and I can't help it if you like.
Conclusion: an interface class is the leader of a class, pointing the way, and subclasses must complete it to specify methods.
PHP abstract class: abstract
Is an abstract class and interface class some like, remember where I saw it in a word, an abstract class is the class as part of the draw out, it looks very funny, it spoke the truth of the abstract class, the role of abstract classes is that when you found that many of your class written in many ways you constantly repeated, you can consider to use abstract classes, you may say "I'm not every public class can override a class I instantiate an the public class, it is ok to call the same method", this is can, in fact an abstract class work is also this, but he saves you instantiate this step, It makes it as easy for you to call this class method directly, and you can also override this method. Such as:
 
abstract class BaseShop 
{ 
public function buy($gid) 
{ 
echo(' You have purchased ID for  :'.$gid.' The goods '); 
} 
public function sell($gid) 
{ 
echo(' You sell ID for  :'.$gid.' The goods '); 
} 
public function view($gid) 
{ 
echo(' You looked at the ID for  :'.$gid.' The goods '); 
} 
} 
class BallShop extends BaseShop 
{ 
var $itme_id = null; 
public function __construct() 
{ 
$this->itme_id = 2314; 
} 
public function open() 
{ 
$this->sell($this->itme_id); 
} 
} 

Here is an example to the above as I class defines a shop, take it all as part of the buy (buy), sell (sell), the (view), and implements these methods in the abstract class, then inherit its subclasses are automatically obtained these methods, subclasses can do its own unique, introducing the repetition code, enhance reusability.
Conclusion: abstract class is a class of service providers, there are many services, you do not have to use, when you need to use it, if you do not feel satisfied with the service, you can do it yourself.
Ha ha, the above is my interface to PHP class, abstract class some humble opinions, I hope to make some help to these two friends, if any comments are welcome to leave a message!

Related articles: