PHP Object-oriented-abstract classes and interfaces

Source: Internet
Author: User

I. Abstract classes and Interfaces

1. Abstract classes: Classes that have abstract methods are abstract ( without abstract member properties )

Abstract method: A method without a method body is an abstract method.

What is no method body? No curly braces

All abstract and abstract methods require the abstract keyword decoration

An abstract class can have multiple abstract methods, or you can have multiple non-abstract methods

Abstract methods cannot be declared as private

When inheriting an abstract class, the method also adheres to the 3,2,1 principles

When inheriting an abstract class, the number of formal parameters in the method body must be consistent with the abstract method

When inheriting an abstract class, if the formal parameter in the abstract method has a default value, the implemented method must also have a default value, which can be a different value.

Abstract classes can have member properties

Example: Abstract class demo{

Abstract function Show ($n = ' abc ');

Abstract function test ();

function say () {

echo " green hills do not change the long stream, you will come back!! ";

}

}

Class Son extends demo{

Function Show ($abc =10) {}

function Test () {}

}

$s =new Son;

$s->say ();

2. Interface Technology

Interface Interface

All methods are abstract, this class is interface, interface we do not use class declaration, with interface Declaration

Interfaces cannot have member properties, but can have constants

An interface can be implemented by a class (implements), which must all implement all the abstract methods in the interface

A class can inherit only one parent class (abstract class), but multiple interfaces may be implemented

A class can implement an interface in addition to inheriting it.

A class is the implementation of an interface after first inheriting

Interfaces and interfaces can also inherit

Example: interface demo{

function Show ();

function test ();

}

Interface Demo2 extends demo{

function Show2 ();

}

Class Father {

function say () {

Echo ' said ';

}

Function Show () {

}

}

Class Son extends Father implements demo2{

Function Show () {

}

function Test () {

}

function Show2 () {

}

}

$s =new Son;

$s->say ();

PHP Object-oriented-abstract classes and interfaces

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.