An analysis of problems in PHP about abstract classes and abstract methods

Source: Internet
Author: User
Tags abstract define abstract

  This article mainly introduces the problem analysis of abstract and abstract methods in PHP, and some friends need to refer to it.

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? The only way we define a method name without a method body in a class is an abstract method, the absence of a method is a declaration of a method without braces and the contents of it, but rather a direct declaration after the method name followed by a semicolon, in addition to the Declaration of abstract methods with a keyword "abstract" to decorate.     1, abstract keywords: abstract   Abstract is not accurate 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, the definition of abstract methods and abstract classes   at least one of the methods in a class is abstract, which we call abstract classes. So if defining an abstract class first defines an abstract method.     Code as follows: Abstract class class1{                      &N Bsp                           abstract function fun1 () nbsp ...... }     1, the class has at least one abstract method 2, the abstract method does not allow {} 3, the abstract method must precede with abstract     3, abstract classes and methods use the rules of the     abstract class features:   1 , cannot be instantiated, can only be inherited   2, the inherited derived classes should have all abstract method overloads to instantiate       the declaration on abstract methods is as follows: The   code is as follows: <?php abstract function f UN1 ();  ?>         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 also use theKeyword "abstract" to decorate, an abstract class cannot instantiate an object, so the abstract method is used as a template for the subclass method overload, and the methods in the inherited abstract class are implemented.   Examples of implementations of abstract classes and abstract classes are as follows:     code is as follows: <?php abstract class user{ //define abstract classes     abstract protected function GetUser (); Define abstract methods     Public Function print_content () {        print $this->getuser ();   &nbs P }   Class Vipuser extends user{    protected function GetUser () {        return "abstract class Www.jb51.net with abstract methods ";    }   $user =new vipuser (); Instantiate subclasses $user->print_content (); Abstract classes and abstract methods?>       NOTE: When an abstract class inherits another abstract class (the intent is to extend to that 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 as follows: <?php abstract class user{ protected static   $sal =0;  static abstract function getsal (); Nbsp;static abstract function Setsal ($sal); } class Vipuser extends user{ static function getsal () {  RET Urn self:: $sal; &NBSP}  static function SetsaL ($sal) {  self:: $sal = $sal; &nbsp}} vipuser::setsal (100); echo "You are Www.jb51.net". 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.