PHP interface, abstract class, Final, static example

Source: Internet
Author: User
Tags abstract static class

1. Interface
(1) The use of the interface is through the keyword implements
(2) The interface cannot define a member variable (including a class static variable) and can define a constant
(3) Subclasses must implement all the methods defined by the interface
(4) An interface can only define a method that cannot be implemented
(5) interface has no constructors
(6) The method in the interface and the class that implements it are all public types by default
2. Abstract class
(1) The use of abstract classes is through the keyword extends
(2) cannot be instantiated, you can define the methods that subclasses must implement
(3) Subclasses must define all abstract methods in the parent class, which must have the same access control as in the parent class (or more relaxed)
(4) If there is an abstract method in a class, the class must be defined as an abstract class
(5) Abstract classes can have constructors
(6) Methods in an abstract class can be decorated with private,protected,public.
(7) A class can implement multiple interfaces at the same time, but a class can only inherit from an abstract class.
3. Final class/method
(1) Final class cannot be inherited
(2) Final method cannot be rewritten
4. Static Class/method
(1) can be accessed directly without instantiating the class
(2) A static property cannot be accessed by an object through the-> operator by::

Example

<?php
# interface
Interface human{
Const TEST_CONST = "TEST const"; Defining constants
Public $v; Error, cannot define variable
Static $count; Error, cannot define variable
Public function speak ();
Public function walk ();
Public function run ();
}

# abstract class
Abstract class Father implements human{

Public Function __construct () {
echo "Father Init n";
}

Abstract public Function walk (); Abstract methods

Public Function speak () {
echo "Father speak skill N";
}

Public Function run () {
echo "Father run skill N";
}
}

# Non-abstract class
Class Mother implements human{

Public Function __construct () {
echo "Mother init n";
}

# The Walk method must be implemented here
Public Function Walk () {
echo "Mother walk skill N";
}

Public Function speak () {
echo "Mother speak skill N";
}

Public Function run () {
echo "Mother run skill N";
}
}

Class Son extends father{

Public Function Walk () {
echo "Son walk skill. n ";
}

Public Function speak ($name = ' ") {
echo "Son:". $name. "Speak skill. n ";
}

# Access control must be the same as in the parent class (or more relaxed)
protected function Sport () {
echo "Son sport skill. n ";
}

Final public Function Notteach () {
Echo ' son has not to teach skill ';
}
}

Class Daughter extends mother{

Public function Run ($name = ') {
echo "daughter run skill. n ";
}

}

Final class grandchild extends son{

# Access control must be the same as in the parent class (or more relaxed)
Public Function Sport () {
echo "grandchild sport skill. n ";
}

# cannot override final method Son::notteach ()
Public Function Notteach () {}//Error
}

# class Orphan may isn't inherit from final Class (grandchild)
Class Orphan extends grandchild{}//Error

$son = new Son ();
$son->speak ("Suly");

$daughter = new daughter ();
$daughter->run (' Lily ');

$grandChild = new grandchild ();
$grandChild->sport ();

The difference between an abstract class and an interface

1, the use of the interface is through the keyword implements. The use of abstract classes is extends by keyword. Of course, interfaces can also be inherited by keyword extends.
2. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. You can declare various types of member variables in an abstract class to implement the encapsulation of the data. (The member variables in the other Java interface are declared as public static final types)
3, the interface does not have constructors, abstract classes can have constructors.

4, the methods in the interface are public types by default, and methods in the abstract class can be decorated with private,protected,public.
5. A class can implement multiple interfaces at the same time, but a class can only inherit from an abstract class.

An abstract class or an interface.
If you want to create a model that will be adopted by tightly related objects, you can use abstract classes. If you want to create a feature that will be taken by some unrelated objects, use an interface.
If you must inherit behavior from multiple sources, use an interface.
If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.

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.