The concepts and differences between PHP abstract classes and interfaces
Release: 2016-07-25 09:07:38
Original
1230 people have browsed it
-
-
//1. Define some methods, and the subclass must fully implement all methods in this abstraction - //2. Objects cannot be created from abstract classes. The meaning of this is Being extended
- //3. Abstract classes usually have abstract methods, and there are no braces in the methods
- //4. Abstract methods do not have to implement specific functions, and are completed by subclasses
- //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. The method of the abstract class can have parameters or be empty
- //7. If the abstract method has parameters, then the implementation of the subclass also Must have the same number of parameters
///////////////////////////////Interface class Definition:
- interface Shop{
- public function buy($gid);
- public function sell($gid);
- abstract function view($gid);
- }
- //If you want to use the interface, you must define all the parameters in the interface class No method can be missing (except abstract).
- //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!
- //Example: Implement the above A method of the interface
- class BaseShop implements Shop{
- public function buy($gid){
- echo 'You purchased the product with the ID:' . $gid . '';
- }
- public function sell($gid){
- echo 'You purchased and sold products with ID:' . $gid . '';
- }
- public function view($gid){
- echo 'You browsed products with ID:' . $gid . '';
- }
- }
//Example of multiple inheritance of interface:
- interface staff_i1{ //Interface 1
- function setID();
- function getID();
- }< /p>
interface staff_i2{ //Interface 2
- function setName();
- function getName();
- }
class staff implements staff_i1,staff_i2{
- private $id ;
- private $name;
- function setID($id){
- $this->id = $id;
- }
function getID(){
- return $this->id ;
- }
function setName($name){
- $this->name = $name;
- }
function getName(){
- return $this->name;
- }
function otherFunc(){ //This is a method that does not exist in the interface
- echo “Test”;
- }
- }
- ?>< ;/p>
-
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31