1.php interface class: interface
In fact, their function is very simple. When many people develop a project together, they may all call some classes written by others. Then you will ask, how do I know one of his How is the function implementation method named? At this time, the PHP interface class comes into play. When we define an interface class, the methods in it must be implemented by the following subclasses, such as:
Copy code The code is as follows:
interface Shop
{
public function buy($gid);
public function sell ($gid);
public function view($gid);
}
I declare a shop interface class and define three methods: buy, sell ), look (view), then all subclasses that inherit this class must implement any of these three methods. If the subclass does not implement these, it will not work. In fact, the interface class is, to put it bluntly, the template of a class and the regulations of a class. If you belong to this category, you must follow my regulations. No matter how you do it, I don’t care how you do it. That’s up to you. Things like:
Copy code The code is as follows:
class BaseShop implements Shop
{
public function buy($gid)
{
echo('You bought the product with ID: '.$gid.');
}
public function sell($gid)
{
echo('You sold the product with the ID: '.$gid.');
}
public function view($gid)
{
echo('You viewed Product with ID: '.$gid.');
}
}
Think about it, in a large project where multiple people collaborate, having an interface class is How convenient, so you don't have to ask others what the method name of your certain function is. Of course, if you like this, I can't help it.
Conclusion: The interface class is the leader of a class, pointing out the direction, and the subclass must complete its designated method.
2.php abstract class: abstract
In fact, abstract classes and interface classes are partially similar. I remember seeing this sentence somewhere, abstract classes take out the similar parts. This sentence looks very funny. In fact, it tells the truth about abstract classes. The role of abstract classes is that when you find that many of your classes use many methods that you are constantly writing repeatedly, then you can consider using abstract classes. You may say, "I Isn't it possible to rewrite a class and instantiate one public class for each public class and call the same method?" It is possible here. In fact, this is what the abstract class does, but it saves you the need to instantiate it. This step of transformation makes it as convenient as calling the method of this class directly, and you can also overload this method. For example:
Copy code The code is as follows:
abstract class BaseShop
{
public function buy($gid )
{
echo('You purchased the product with ID: '.$gid.');
}
public function sell($gid)
{
echo ('You sold the product with the ID: '.$gid.');
}
public function view($gid)
{
echo('You viewed the product with the ID:' .$gid.'s');
}
}
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. Like the above, I defined a store class and extracted all its parts like buy, sell and view. , and these methods are implemented in the abstract class, then the subclass that inherits it will automatically obtain these methods, and the subclass will do its own unique things, introduce duplication of code, and improve reusability.
Conclusion: An abstract class is a service provider of a class. It has many services. You don’t have to use them. You can use them when needed. If you feel dissatisfied with not providing services, you can do it yourself.
Haha, the above are my humble opinions on PHP interface classes and abstract classes. I hope it can help those who are confused about these two. If you have any comments, please leave a message!
http://www.bkjia.com/PHPjc/320804.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320804.htmlTechArticle1.php Interface class: interface In fact, their role is very simple. When many people develop a project together, Maybe they will call some classes written by others, then you will ask, how do I know them...