Php object-oriented interface and polymorphism usage

Source: Internet
Author: User
Tags inheritance

Usage of interfaces and polymorphism in object-oriented php tutorials
The method name of the interface is show. The class that inherits the interface must have the show method. Otherwise, an error is reported. That is to say, the interface method is false. What really works is the method in the inherited class. This is because of this, so I think the abstract class of the interface root php is a bit like.

& Lt ;? Php
Interface face1
{
Const param = 'test ';
Public function show ();
}

Class test implements face1
{
Public function show ()
 {
Echo "interface is run & lt; br & gt ;";
 }
}

$ Face = new test ();
Echo $ face-& gt; show (); // inerface is run
Echo face1: param; // test
? & Gt;


Inter-interface inheritance and calling interface passing parameters

& Lt ;? Php
Interface face1
{
Public function show ();
}

Interface face2 extends face1
{
Public function show1 (test1 $ test, $ num );
}

Class test implements face2
{
Public function show ()
 {
Echo "OK & lt; br & gt ;";
 }

Public function show1 (test1 $ test, $ num)
 {
Var_dump ($ test );
Echo $ test1-& gt; aaaa. "$ num & lt; br & gt ;";
 }
}

Class test1
{
Public $ aaaa = "this is a test ";
Function fun (){
Echo '=====================& lt; br & gt ;';
 }
}

$ Show = new test1;
$ Show-& gt; fun (); & nbsp; & nbsp; // Display ========================
Test: show (); & nbsp; & nbsp; // Display OK
Test: show1 ($ show, 6); & nbsp; // object (test1) #1 (1) {["aaaa"] = & gt; & nbsp; string (14) "this is a test"} 6
? & Gt;

/* Interface technology
 *
* An interface is a special abstract class and an abstract class.
 *
* Interfaces and abstract classes play the same role.
 *
* Because PHP is a single-inherited class, if an abstract class is used, the subclass can no longer inherit other classes.
 *
* If you want to implement some specifications and inherit other classes. An interface is required.
 *
* Comparison between interfaces and abstract classes
 *
* 1. Objects cannot be created for the same purpose. All objects must be implemented by subclasses.
 *
* 2. The interface description is different from the abstract class.
 *
* 3. The interface is implemented differently.
 *
* 4. All methods in the interface must be abstract methods. Only abstract methods can be declared (abstract modification is not required)
 *
* 5. The member attributes in the interface can only declare constants, but cannot declare variables.
 *
* 6. All access permissions of the interface members must be public. The minimum permission in the abstract class is protected.
 *
* Declared interface: interface name {};
 *   
* 7. Use a class to implement the interface. Instead of using extends, use the implements keyword.
 * 
* If the subclass is to override the abstract methods in the parent interface, use implements (implementation), class -- interface, abstract class -- interface to use implements, and interface -- interface to use extends (inheritance)
 *
* Some methods in the interface can be implemented using abstract classes.
* If you want subclass to create objects, you must implement all methods in the interface.
* You can define one interface to inherit another interface.
* A class can implement multiple interfaces (develop subclass according to multiple specifications) and separate multiple interface names with commas (,).
* A class can implement one or more interfaces while inheriting a class.
 *
* Implements is used for two purposes:
 *
* 1. Multiple interfaces can be implemented, while extends can inherit only one parent class.
 * 
* 2. If you do not use the extends word, you can inherit a class, so you can use both of them at the same time.
 *
* Polymorphism: polymorphism is one of the three main characteristics of object-oriented
 *
* "Polymorphism" is an important feature of object-oriented design. It shows the dynamic binding function, also known as "Polymorphism ). Multi-State functions enable full extension during software development and maintenance ). In fact, the most direct definition of polymorphism is to allow different class objects with inheritance relationships to call member functions with the same name to produce different responses.
 *
 *
 *
 *
 *
*/
// Declare the interface
Interface Demo {
  
Const HOST = "localhost ";

Const USER = "admin ";

Function fun1 (); // do not add abstract to the declaration method. The default value is. The permission is public.
Function fun2 ();
 }
 
// Interface inheritance
Interface Demo2 extends Demo {
Function fun3 ();
Function fun4 ();
 }

Interface Demo3 {
Function fun5 ();
Function fun6 ();
 }

Interface Demo4 {
Function fun7 ();
 }

Echo Demo: HOST; // constant in the interface that can be accessed

Class Hello {
Function fun8 (){

 }
}

// Subclass must implement all methods in the interface
Class UTest extends Hello implements Demo2, Demo3, Demo4 {// implement multiple interfaces
 
Function fun1 (){

 }

Function fun2 (){

 }

Function fun3 (){

 }

Function fun4 (){

 }

Function fun5 (){

 }

Function fun6 (){

 }

Function fun7 (){

 }

}


/* ------------------- Polymorphism ---------------*/

Interface Test {

Function fun1 ();

Function fun2 ();
}

Class One implements Test {
Function fun1 (){
Echo "aaaaaaaaa ";
 }

Function fun2 (){
Echo "bbbbbbbbbbbbbb ";
 }
}

Class Two implements Test {
 
Function fun1 (){
Echo & quot; 11111111 & quot ";
 }

Function fun2 (){
Echo & quot; 2222222222 & quot ";
 }
}

// The same interface implements the same method, different objects, and different outputs. This is the manifestation and application of polymorphism.

$ Test = new One;
$ Test-> fun1 (); // output a row
$ Test-> fun2 (); // output a row of B

$ Test = new Two;
$ Test-> fun1 (); // output a row of 1
$ Test-> fun2 (); // output a row of 2


?>

<? Php
/* -------------- Use a multi-state application instance to simulate USB devices ------------------*/

// A usb interface
Interface USB {
Function mount (); // method for loading USB
Function work (); // how to work with USB
Function unmount (); // method for detaching a USB
}

// Define a USB flash drive
Class Upan implements USB {// implement USB interface
Function mount (){
Echo "the USB flash disk is successfully loaded <br/> ";
 }

Function work (){
Echo "U disk started to work <br/> ";
 }

Function unmount (){
Echo "the USB flash drive has been uninstalled <br/> ";
 }
}

// Define a USB device USB mouse
Class Umouse implements USB {// implement USB interface
Function mount (){
Echo "USB keyboard loaded <br/> ";
 }

Function work (){
Echo "USB keyboard started to work <br/> ";
 }

Function unmount (){
Echo "USB keyboard unmounted successfully <br/> ";
 }
}

// Define a computer
Class Computer {
// How to use a USB device
Function useUSB ($ usb) {// $ usb parameter indicates which USB device is used
$ Usb-> mount (); // call the device loading method
$ Usb-> work (); // call the working method of the device
$ Usb-> unmount (); // call the device uninstall method
 }
 
}

// Define the category of a computer user
Class PcUser {
// Install USB
Function install (){
  
// Get a computer first
$ Pc = new Computer;
 
// Get Some USB devices
$ Up = new Upan; // Get a USB flash drive
$ Um = new Umouse; // Get a USB mouse

// Insert the USB device into the computer and use the USB device method in the computer to call the device to be inserted
$ Pc-> useUSB ($ up); // Insert a USB flash drive
$ Pc-> useUSB ($ um); // Insert a USB mouse
 }

}

// Instantiate a computer user
$ User = new PcUser;

$ User-> install (); // install the device

/* ------------- Output content --------------
USB flash disk mounted successfully
U disk started to work
USB flash disk unmounted
USB keyboard mounted
USB keyboard started to work
USB keyboard uninstalled


From the above example, we can see that an interface can be inherited by multiple classes, and the class names are different. The same class can be called each other, but different classes cannot. Echo $ first_string-> compare ($ second_int); the fatal error is returned.

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.