Home > Backend Development > PHP Tutorial > The concepts and differences between PHP abstract classes and interfaces

The concepts and differences between PHP abstract classes and interfaces

WBOY
Release: 2016-07-25 09:07:38
Original
1230 people have browsed it
  1. //1. Define some methods, and the subclass must fully implement all methods in this abstraction

  2. //2. Objects cannot be created from abstract classes. The meaning of this is Being extended
  3. //3. Abstract classes usually have abstract methods, and there are no braces in the methods
  4. //4. Abstract methods do not have to implement specific functions, and are completed by subclasses
  5. //5. Implement methods of abstract classes in subclasses When, the visibility of its subclass must be greater than or equal to the definition of the abstract method
  6. //6. The method of the abstract class can have parameters or be empty
  7. //7. If the abstract method has parameters, then the implementation of the subclass also Must have the same number of parameters

  8. ///////////////////////////////Interface class Definition:

  9. interface Shop{
  10. public function buy($gid);
  11. public function sell($gid);
  12. abstract function view($gid);
  13. }
  14. //If you want to use the interface, you must define all the parameters in the interface class No method can be missing (except abstract).
  15. //In this way, if in a large project, no matter how others do the following method, they must implement all the methods in this interface!

  16. //Example: Implement the above A method of the interface
  17. class BaseShop implements Shop{
  18. public function buy($gid){
  19. echo 'You purchased the product with the ID:' . $gid . '';
  20. }
  21. public function sell($gid){
  22. echo 'You purchased and sold products with ID:' . $gid . '';
  23. }
  24. public function view($gid){
  25. echo 'You browsed products with ID:' . $gid . '';
  26. }
  27. }

  28. //Example of multiple inheritance of interface:

  29. interface staff_i1{ //Interface 1
  30. function setID();
  31. function getID();
  32. }< /p>
  33. interface staff_i2{ //Interface 2

  34. function setName();
  35. function getName();
  36. }

  37. class staff implements staff_i1,staff_i2{

  38. private $id ;
  39. private $name;
  40. function setID($id){
  41. $this->id = $id;
  42. }

  43. function getID(){

  44. return $this->id ;
  45. }

  46. function setName($name){

  47. $this->name = $name;
  48. }

  49. function getName(){

  50. return $this->name;
  51. }

  52. function otherFunc(){ //This is a method that does not exist in the interface

  53. echo “Test”;
  54. }
  55. }
  56. ?>< ;/p>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template