Detailed explanation of PHP object-oriented interface technology examples

伊谢尔伦
Release: 2023-03-11 22:02:01
Original
1195 people have browsed it

PHP5 Interface Technology

PHP, like most Object-orientedprogramming languages, does not support multiple inheritance. That is to say, each A class can only inherit from one parent
class. In order to solve this problem, PHP introduced interfaces. The idea of ​​​​the interface is to specify a series of methods that a class that implements the interface must
implement. The interface is a special abstract class, and the abstract class is a special class, so the interface is also a
special class. Why is the interface said to be a special abstract class? If all the methods in an abstract class are
abstract methods, then we change the declaration method to use "interface"; that is to say, all the methods in the interface must be
declared as abstract methods. In addition Variables cannot be declared in the interface, and all members in the interface have public permissions.
So subclasses must also use public permissions when implementing them.
The keyword we use when declaring a class is "class", and the interface is a special class, and the keyword
used is "interface";
Definition of class: class class name {… }, interface declaration: interface interface name { ... }
Code snippet

<?php
abstract class Demo{ 
var $test; 
abstract function fun1(); 
abstract function fun2(); 
function fun3(){ 
… . 
} 
} 
$demo=new Demo(); //抽象类为能产生实例对象,所以这样做是错的,实例化对象交给子类 
class Test 
extends
 Demo{ 
function fun1(){ 
… 
} 
function fun2(){ 
… 
} 
} 
$test=new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法 
?>
Copy after login

In the above example, an interface "one" is defined, which declares two abstract methods "fun1" and "fun2", Because all the methods in the interface are abstract methods, there is no need to use the
"abstract" keyword like an abstract class when declaring an abstract method. This keyword is already added by default. In addition, The "public" access permission in the interface can also be removed, because the default is public, because all members in the interface must be public, so we cannot use "private" for members in the interface and "protected" permissions, use public or the default
. In addition, we also declared a constant "constant" in the interface. Because variables cannot be used as members in the interface, we have to use the const keyword declaration.
Because the interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot produce real
objects; it is also a specification, and all abstract methods need to be implemented by subclasses .
We can use the "extends" keyword to let one interface inherit another interface;
Code snippet

<?php 
//使用”extends”继承另外一个接口 
interface Two extends One{ 
function fun3(); 
function fun4(); 
} 
?>
Copy after login

And we define a subclass of an interface to implement the key to using all abstract methods in the interface The word is "implements", and
is not the "extends" we mentioned earlier;
Code snippet

<?php 
//使用“implements”这个关键字去实现接口中的抽象方法 
class Three implements One{ 
function fun1(){ 
… . 
} 
function fun2(){ 
… . 
} 
} 
//实现了全部方法,我们去可以使用子类去实例化对象了 
$three=new Three(); 
?>
Copy after login

We can also use abstract classes to implement some abstract methods in the interface, but we must If you want to instantiate an object, this abstract
class must have a subclass to implement all its abstract methods;
As we said before, PHP is single inheritance, and a class can only have one parent class. But a class can implement multiple interfaces, which is equivalent to a class having to comply with multiple specifications, just like we must not only abide by the laws of the country, but also

if we are in school, we must abide by the school rules. ;

Code snippet

<?php 
//使用implements实现多个接口 
class Four implemtns 接口一, 接口二, … .{ 
//必须把所有接口中的方法都要实现才可以实例化对象。 
} 
?>
Copy after login

In PHP, not only one class can implement multiple interfaces, but you can also implement multiple interfaces while inheriting a class. You must
inherit the class first and then implement the interface;
Code snippet
<?php 
//使用extends继承一个类,使用implements实现多个接口 
class Four extends 类名一implemtns 接口一, 接口二, … .{ 
//所有接口中的方法都要实现才可以实例化对象 
… … … .. 
} 
?>
Copy after login


The above is the detailed content of Detailed explanation of PHP object-oriented interface technology examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!