Abstract and abstract methods in PHP

Source: Internet
Author: User
Tags abstract

In an object-oriented (OOP) language, a class can have one or more subclasses, and each class has at least one interface that the public method accesses as external code. and abstract methods are introduced to facilitate inheritance, now look at how abstract classes and abstract methods are defined and their characteristics.

What is an abstract method? What we define in the class is that the method name has no method body is an abstract method, the so-called no method body is there is no curly braces and the contents of the method declaration, but rather a direct declaration after the method name followed by a semicolon, plus a keyword when declaring an abstract method. Abstract "to decorate.

1, abstract key words: abstract

Abstraction is not an exact description, but there is a certain concept or name, in PHP to declare an abstract class or method we need to use the Adstract keyword.

2. Definitions of abstract methods and abstract classes

At least one of the methods in a class is abstract, which we call an abstract class. So if defining an abstract class first defines an abstract method.

Abstract class class1{

Abstract function fun1 ();

......

}

1. There is at least one abstract method in the class

2, the abstract method does not allow {}

3, the abstract method must be added abstract before

3. Abstract class and method usage rules

Several features of abstract classes:

1, cannot be instantiated, can only be inherited

2, in the inherited derived class, all abstract method overloads are to be instantiated

The Declaration on abstract methods is as follows:

The code is as follows

<?php
Abstract function fun1 ();

?>

What is an abstract class? As long as one of the classes has an abstract method, the class must be defined as an abstract class. Abstract classes are also decorated with the keyword "abstract" and abstract classes cannot instantiate objects, so the abstract method is used as a template for the subclass method overload and implements the methods in the inherited abstract class.

Examples of implementations of abstract classes and abstract classes are as follows:

The code is as follows

<?php
Abstract class user{//Defining abstraction Classes
Abstract protected function getuser (); Defining abstract methods
Public Function print_content () {
Print $this->getuser ();
}
}

Class Vipuser extends user{
protected function GetUser () {
Return "abstract class and abstract method";
}
}

$user =new Vipuser (); Instantiating subclasses
$user->print_content (); Abstract classes and abstract methods
?>

Note: When an abstract class inherits another abstract class (the intent is to extend the abstract class), the abstract method of the parent class cannot be overridden.

In PHP5.1, static abstract methods are supported in abstract classes. The following example sees a static abstract method that can be declared. When you implement this method, you must be a static method.

  code is as follows &nbs P;

<?php
Abstract class user{
 protected static  $sal = 0;
 static abstract function getsal ();
 static abstract function setsal ($sal);  
}
Class Vipuser extends user{
 static function ge Tsal () {
  return self:: $sal;
 }
 static function Setsal ($sal) {
  self:: $sal = $sal;
 }
}
Vipuser::setsal;
echo "You are Sal is". Vipuser::getsal ();
?

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.