SoFunction
Updated on 2025-03-02

Detailed explanation of the principles and usage of abstract (abstract), final (final) and static (static) in PHP

This article describes the principles and usage of abstract, final and static in PHP. Share it for your reference, as follows:

abstract(abstract)

PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. If at least one method in any class is declared abstract, then the class must be declared abstract. A method defined as an abstract just declares its calling method (parameter).Its specific functional implementation cannot be defined

When inheriting an abstract class, the subclass must define all abstract methods in the parent class.;in addition,These methods must have access control the same as in the parent class (or more relaxed). For example, an abstract method is declared as protected, then the methods implemented in the subclass should be declared as protected or public (strictness: private>protected>public), and cannot be defined as private. alsoThe method must be called, that is, the type and the number of required parameters must be the same.. For example, if a subclass defines an optional parameter and does not exist in the declaration of the parent class abstract method, the declarations of the two do not conflict. This also applies to constructors since PHP 5.4. The constructor declaration before PHP 5.4 can be different.

Summarize:

  1. Abstract classes cannot be instantiated;
  2. If there are any abstract methods in the class, then this class must also be abstract;
  3. Abstract classes can only declare call methods and parameters, and cannot define specific functional implementations;
  4. Subclasses that inherit abstract classes must implement all abstract methods of abstract classes;
  5. The access control of abstract methods implemented in subclasses must be stricter than that of parent class;
  6. The method and number of parameters of the method implemented in the subclass must be consistent with the method implemented.

example:

<?php
abstract class AbstractClass
{
  // Mandatory subclasses to define these methods, without defining functional implementations  abstract protected function getValue();
  abstract protected function prefixValue($prefix);

  // Ordinary method (non-abstract method), subclasses can be not overwritten  public function printOut() {
    print $this->getValue() . "\n";
  }
}

class ConcreteClass1 extends AbstractClass
{
  protected function getValue() {
    return "ConcreteClass1";
  }

  public function prefixValue($prefix) {
    return "{$prefix}ConcreteClass1";
  }
}

class ConcreteClass2 extends AbstractClass
{
 //The access method can be more relaxed  public function getValue() {
    return "ConcreteClass2";
  }

  public function prefixValue($prefix) {
    return "{$prefix}ConcreteClass2";
  }
}

$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";

$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
?>

<?php
abstract class AbstractClass
{
  // Our abstract method only needs to define the required parameters  abstract protected function prefixName($name);

}

class ConcreteClass extends AbstractClass
{

  // Our subclass can define optional parameters that do not exist in the parent class signature  public function prefixName($name, $separator = ".") {
    if ($name == "Pacman") {
      $prefix = "Mr";
    } elseif ($name == "Pacwoman") {
      $prefix = "Mrs";
    } else {
      $prefix = "";
    }
    return "{$prefix}{$separator} {$name}";
  }
}

$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";
?>

final

If a method in the parent class is declared final, the subclass cannot override the method. If a class is declared final, it cannot be inherited.

This is easier to understand, so I won't go into details

static

Declare the class attributes or methods to be static, and you can access them directly without instantiating the class. Static properties cannot be accessed through an object whose class has been instantiated (but static methods can).

For PHP 4 compatibility, properties and methods default to public if access control is not specified.

Since static methods do not need to be called through objects, the pseudo-variable $this is not available in static methods.

Static properties cannot be accessed by objects through the -> operator.

Calling a non-static method in a static way will result in an E_STRICT level error.

Just like all other PHP static variables, static properties can only be initialized as literals or constants and cannot use expressions. Therefore, static properties can be initialized as integers or arrays, but cannot be initialized as another variable or function return value, nor can it point to an object.

Since PHP 5.3.0, a variable can be used to dynamically call classes. But the value of this variable cannot be the keyword self, parent or static.

Summarize:

  1. Static methods do not need to be instantiated and can be accessed directly;
  2. The object instantiated by the class cannot access static properties in the class, but can access static methods;
  3. The pseudo-variable $this is not available in static methods;
  4. Static properties cannot be accessed by objects through the -> operator;
  5. Calling a non-static method in a static way will result in an E_STRICT level error;
  6. Static properties can only be initialized as literals or constants, and expressions cannot be used (function returns a value/norm a variable/object);
  7. A variable can be used to call the class dynamically. But the value of this variable cannot be the keyword self, parent or static.
<?php
class Foo
{
  public static $my_static = 'foo';

  public function staticValue() {
    return self::$my_static;
  }
}

class Bar extends Foo
{
  public function fooStatic() {
    return parent::$my_static;
  }
}

print Foo::$my_static . "\n";

$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";   // Undefined "Property" my_static 

print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0

print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
  </programlisting>
 </example>

 <example>
  <title>Static method example</title>
  <programlisting role="php">
<![CDATA[
<?php
class Foo {
  public static function aStaticMethod() {
    // ...
  }
}

Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // Starting from PHP 5.3.0?>

For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.