PHP Object-oriented programming (OOP) learning notes (i)-abstract classes, object interfaces, instanceof, and contract programming

Source: Internet
Author: User

1. Abstract class in PHP

PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation. Use the abstract modifier in the declaration of a class to declare a class as abstract.

It can be understood that abstract classes, as a base class, leave specific details to the inheritors to implement. Abstract concepts allow you to create well-scaled schemas in your development projects.


Abstract class AbstractClass
{
Code ...
}

1.1. Abstract methods

Define an abstract method using the abstract keyword. Abstract methods retain only the method prototype (the definition of the method rejects the signature after the method body), which includes access levels, function keywords, function names, and parameters. He does not contain ({}) or any code inside the parentheses. For example, the following code is an abstract method definition:

The code is as follows:
Abstract public Function prototypename ($protoParam);

When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. The method must be called in the same way, i.e. the type and the required number of parameters must be the same.

1.2. About Abstract class

A class must be declared as an abstract class as long as it contains at least one abstract method
Methods that are declared abstract must contain the same or lower access levels at the time of implementation.
You cannot use the New keyword to create an instance of an abstract class.
A method that is declared abstract cannot contain a function body.
If you declare an extended class as an abstract class, you can extend the abstract class without implementing all of the abstract methods. (If a class inherits from an abstract class, it must also be declared abstract when it does not implement all the abstract methods declared in the base class.) )
1.3. Use abstract classes

The code is as follows:
<?php
Abstract class Car
{
Abstract function getmaxspeend ();
}
Class Roadster extends Car
{
Public $Speend;
Public Function setspeend ($speend = 0)
{
$this->speend = $speend;
}
Public Function Getmaxspeend ()
{
return $this->speend;
}
}
Class Street
{
Public $Cars;
Public $SpeendLimit;
function __construct ($speendLimit = 200)
{
$this-speendlimit = $speendLimit;
$this-Cars = Array ();
}
protected function Isstreetlegal ($car)
{
if ($car->getmaxspeend () < $this-Speendlimit)
{
return true;
}
Else
{
return false;
}
}
Public Function Addcar ($car)
{
if ($this->isstreetlegal ($car))
{
Echo ' The Car was allowed on the road.
$this->cars[] = $car;
}
Else
{
Echo ' The Car is too fast and being not allowed on the road.
}
}
}
$Porsche 911 = new Roadster ();
$Porsche 911->setspeend (340);
$FuWaiStreet = new Street (80);
$FuWaiStreet->addcar ($Porsche 911);
/**
*
* @result
*
* The Car is too fast and being not allowed on the road. [Finished in 0.1s]
*
*/
?>



2. Object Interface

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.

Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.

All methods defined in the interface must be public, which is the attribute of the interface.

An interface is a class-like structure that declares a method that an implementation class must declare. For example, an interface is often used to declare an API without having to define how to implement the API.

Most developers choose to prefix the interface name with the capital letter I, which distinguishes it from the class in the code and in the generated document.

2.1 Interface Implementation (implements)

To implement an interface, using the implements operator (which inherits the abstract class requires a different extends keyword), the class must implement all the methods defined in the interface, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

When implementing multiple interfaces, the methods in the interface cannot have duplicate names.
Interfaces can also be inherited by using the extends operator.
Class to implement an interface, it must be in a way that is exactly the same as the method defined in the interface. Failure to do so can result in fatal errors.
Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces.
2.2 Examples of using interfaces


<?php
Abstract class Car
{
Abstract function setspeend ($speend = 0);
}
Interface Ispeendinfo
{
function Getmaxspeend ();
}
Class Roadster extends Car implements Ispeendinfo
{
Public $Speend;
Public Function setspeend ($speend = 0)
{
$this->speend = $speend;
}
Public Function Getmaxspeend ()
{
return $this->speend;
}
}
Class Street
{
Public $Cars;
Public $SpeendLimit;
function __construct ($speendLimit = 200)
{
$this-speendlimit = $speendLimit;
$this-Cars = Array ();
}
protected function Isstreetlegal ($car)
{
if ($car->getmaxspeend () < $this-Speendlimit)
{
return true;
}
Else
{
return false;
}
}
Public Function Addcar ($car)
{
if ($this->isstreetlegal ($car))
{
Echo ' The Car was allowed on the road.
$this->cars[] = $car;
}
Else
{
Echo ' The Car is too fast and being not allowed on the road.
}
}
}

$Porsche 911 = new Roadster ();
$Porsche 911->setspeend (340);
$FuWaiStreet = new Street (80);
$FuWaiStreet->addcar ($Porsche 911);
/**
*
* @result
*
* The Car is too fast and being not allowed on the road. [Finished in 0.1s]
*
*/
?>

3. type operator instanceof

The instanceof operator is a comparison operator in PHP5. He accepts both left and right arguments and returns a Boolean value.

Determine if a PHP variable belongs to an instance of a class class
Checks whether an object is inherited from a type
Checks if an object belongs to an instance of a class
Determine if a variable is an instance of an object that implements an interface


Echo $Porsche 911 instanceof Car;
Result:1

Echo $Porsche 911 instanceof Ispeendinfo;
Result:1

4. Contract-type programming

Contract design or design by contract (DbC) is a method of designing computer software. This approach requires software designers to define formal, accurate, and verifiable interfaces for software components, which adds a priori, posteriori, and invariant to the traditional abstract data types. The "contract" or "contract" used in the name of this method is a metaphor, as it is somewhat similar to a commercial contract.

A programming practice to implement a declarative interface before writing a class. This method is useful in guaranteeing the encapsulation of a class. Using the contractual programming technique, we can define the function of the view implementation before creating the application, which is very similar to the way the architect paints the blueprint before building it.

5. Summary

Abstract classes are classes declared using the abstract keyword. By marking a class as an abstract class, we can defer implementing the declared method. To declare a method as an abstract method, simply remove the method entity containing all curly braces and end the line of code declared by the method with a semicolon.

Abstract classes cannot be instantiated directly, they must be inherited.

If a class inherits from an abstract class, it must also be declared abstract when it does not implement all the abstract methods declared in the base class.

In an interface, we can declare a method prototype without a method body, which is similar to an abstract class. The difference between them is that an interface cannot declare any method that has a method body, and they use a different syntax. To force the uncover rule to be added to a class, we need to use the Implements keyword instead of the extends keyword.

In some cases we need to determine whether a class is a type of a particular class, or whether a particular interface is implemented. Instanceof is divided into fit to complete this task. Instanceof checks three things: whether the instance is a specific type, whether the instance inherits from a particular type, whether the instance or any of his ancestor classes implement a class-specific interface.

Some languages have the ability to inherit from multiple classes, which is called multiple inheritance. PHP does not support multiple inheritance. Idea, he provides the ability to declare multiple interfaces for a class.

Interfaces are useful when declaring a rule that a class must follow. Contractual programming techniques use this functionality to enhance encapsulation and optimize workflow.

PHP Object-oriented programming (OOP) learning notes (i)-abstract classes, object interfaces, instanceof, and contract programming

Related Article

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.