An abstract function is a method signature that defines a contract in the super (abstract) class. That contract must be implemented by any subclasses. The method implementation's visibility in the subclasses must be the same or a less restrictive one than that of the superclass. Please, take a look at Class Abstraction - PHP Manual.

Note: visibility is not the same as scope. Visibility is about data hiding in the context of OOP. Scope is more general. It's about where (in the code) a variable is defined.

Answer from ranieribt on Stack Overflow
🌐
PHP
php.net › manual › en › language.oop5.abstract.php
PHP: Class Abstraction - Manual
Classes defined as abstract cannot ... Methods defined as abstract simply declare the method's signature and whether it is public or protected; they cannot define the implementation....
🌐
W3Schools
w3schools.com › php › php_oop_classes_abstract.asp
PHP OOP Abstract Classes
The child class method must be defined with the same name and it redeclares the parent abstract method · The child class method must be defined with the same or a less restricted access modifier · The number of required arguments must be the same. However, the child class may have optional arguments in addition ... <?php // Parent class abstract class Car { public $name; public function __construct($name) { $this->name = $name; } abstract public function intro() : string; } // Child classes class Audi extends Car { public function intro() : string { return "Choose German quality!
Top answer
1 of 2
2

An abstract function is a method signature that defines a contract in the super (abstract) class. That contract must be implemented by any subclasses. The method implementation's visibility in the subclasses must be the same or a less restrictive one than that of the superclass. Please, take a look at Class Abstraction - PHP Manual.

Note: visibility is not the same as scope. Visibility is about data hiding in the context of OOP. Scope is more general. It's about where (in the code) a variable is defined.

2 of 2
0

Abstract methods are theoritically used when you want to share a method between inheriting instances. For instance, say you have a abstract class that represents a view, and each inheriting class will have to render something, you can define the method in the parent abstract class, and all child will have access to it:

abstract class Template {
    public function render($template) {
        include($template);
    }
}
class SiteView extends Template {
    protected $title = "default title";
}
$siteView = new SiteView();
$siteView->render('path/to/site/template.html');

To improve this, you can also use an interface and start type-hinting your classes:

interface Renderer {
    public function render($template);
}
abstract class Template implements Renderer {
    public function render($template) {
        include($template);
    }
}
class SiteView extends Template {
    protected $title = "default title";
    protected $body= "default body";
}
class Controller {
    private $view;
    public function __construct(Renderer $view) {
        $this->view = $view;
    }
    public function show() {
        $this->view->render('path/to/site/template.html');
    }
}
$siteView = new SiteView();
$controller = new Controller($siteView);
$controller->show();

Notice how afterwards the controller is decoupled from the abstract and concrete class, while the abstract class allows you to share the render function with inheriting Views. Should you decide to create other abstract class representing other ways to render stuff, the controller would continue working.

for the records, the template would look like:

<!DOCTYPE html>
<html>
<head>
    <title><?= $this->title ?></title>
</head>
<body><?= $this->body ?></body>
</html>
🌐
Phpenthusiast
phpenthusiast.com › learn, practice, and apply object-oriented php › object-oriented php tutorials › abstract classes and methods in php | phpenthusiast
Abstract classes and methods in PHP | PHPenthusiast
February 17, 2017 - Instead, we need to create child classes that add the code into the bodies of the methods, and use these child classes to create objects. ... In order to declare a class as abstract, we need to prefix the name of the class with the abstract keyword.
🌐
GeeksforGeeks
geeksforgeeks.org › php › abstract-classes-in-php
Abstract Classes in PHP - GeeksforGeeks
March 15, 2025 - An abstract class can contain abstract as well as non-abstract methods. ... <?php abstract class Base { abstract function printdata(); function pr() { echo "Base class"; } } class Derived extends Base { function printdata() { echo "Derived class"; } } // Creating an object of Derived class $b1 = new Derived; $b1->printdata(); ?>
🌐
DEV Community
dev.to › patricia1988hernandez2 › abstract-classes-in-php-what-they-are-and-how-to-use-them-20cp
Abstract Classes in PHP: What They Are and How to Use Them - DEV Community
April 22, 2025 - You can't create an object from an abstract class directly. But you can extend it and use its structure. An abstract class contains abstract methods. These are methods declared but not fully defined.
🌐
Tutorialspoint
tutorialspoint.com › php › php_abstract_classes.htm
PHP - Abstract Classes
An abstract class in PHP is a class that cannot be created on its own. This means you can't create objects straight from an abstract class. Abstract classes are intended to be extended by subsequent classes. They serve as a blueprint for other classes, defining the common methods and properties that
Find elsewhere
🌐
Medium
medium.com › @Amir_M4A › in-the-world-of-php-programming-there-are-several-key-concepts-that-developers-need-to-understand-b2ed1916287f
PHP: Abstract Classes, Interfaces, and Traits Explained | Medium
September 23, 2024 - An abstract class in PHP is a class that cannot be instantiated and is meant to serve as a blueprint for other classes to inherit from. It contains abstract methods, which are declared but not implemented within the abstract class itself.
🌐
O'Reilly
oreilly.com › library › view › php-mysql-r › 9780470167779 › 9780470167779_using_abstract_methods_in_abstract_class.html
Using Abstract Methods in Abstract Classes and Interfaces - PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® [Book]
You can use abstract methods that specify the information to be passed, but do not contain any code. Abstract methods were added in PHP 5. You can use abstract methods in abstract classes or in interfaces. An abstract class contains both abstract methods and nonabstract methods.
🌐
dailycomputerscience
dailycomputerscience.com › post › php-oop-fundamentals-abstract-classes-in-php
PHP OOP Fundamentals - Abstract Classes in PHP
October 11, 2024 - An abstract class in PHP is a special type of class that cannot be instantiated on its own. It is designed to be extended by other classes, and it serves as a blueprint for its subclasses.
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › Abstract-Classes
PHP Abstract Classes - abstract keyword, abstract functions | jobtensor
Create 3 child classes extending the abstract class namely: Apple, Orange, Grape. In these child classes, define the color function so that it prints Apple is red for the Apple class, Orange is orange for the Orange class and Grape is purple for the Grape class. <?php <?php abstract class Fruit { public $name; public function __construct($name) { $this->name = $name; } abstract public function color(); } // Child classes class Apple extends Fruit { public function color(){ return "$this->name is red"; } } class Orange extends Fruit { public function color(){ return "$this->name is orange"; } }
🌐
PHP Tutorial
phptutorial.net › home › php oop › php abstract class
PHP Abstract Class
April 7, 2025 - <?php abstract class Dumper { abstract public function dump($data); } class WebDumper extends Dumper { public function dump($data) { echo '<pre>'; var_dump($data); echo '</pre>'; } } class ConsoleDumper extends Dumper { public function dump($data) { var_dump($data); } } class DumperFactory { public static function getDumper() { return PHP_SAPI === 'cli' ? new ConsoleDumper() : new WebDumper(); } } $dumper = DumperFactory::getDumper(); $dumper->dump('PHP abstract class is awesome!');Code language: PHP (php) An abstract class cannot be instantiated. It provides an interface for other classes to extend. An abstract method doesn’t have an implementation.
🌐
DEV Community
dev.to › eelcoverbrugge › php-abstract-classes-10eh
PHP Abstract Classes Explained - DEV Community
October 22, 2021 - But this abstract class on it's own won't do so much. If you would create an User object, you would have a non-functional user. This doesn't exist on our webshop. So in order to make it useful, we create our two types of users: <?php class Customer extends User{ private $id public function __construct($name, $id) { $this->name = $name; $this->id = $id; } public function getUserType() { return 'customer'; } } class Employee extends User{ private $id public function __construct($name, $id) { $this->name = $name; $this->id = $id; } public function getUserType() { return 'employee'; } }
🌐
Medium
medium.com › @andreibirta95 › understanding-and-utilizing-abstract-classes-in-php-7eeeeeabc444
Understanding and Utilizing Abstract Classes in PHP | by Andrei Birta | Medium
January 27, 2023 - An abstract class is used as a blueprint or template for other classes and it contains methods that must be implemented by its subclasses. These methods are known as abstract methods.
🌐
Alex Web Develop
alexwebdevelop.com › home › php abstract classes explained
PHP Abstract Classes Explained - Alex Web Develop
August 21, 2022 - So, let’s start by making it clear what abstract classes actually are. A standard class is like an instruction sheet for PHP. When you create an object of that class, PHP follows the instructions and creates the object. For example, you can write a “Computer” class with attributes such as CPU type, memory and power consumption, and with methods such as turn on, turn off and stand-by.
Top answer
1 of 7
154

An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL methods are abstract.

See this section of the PHP manual for further reference.

2 of 7
140

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

1. Can not instantiate abstract class: Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.

Example below :

abstract class AbstractClass
{

    abstract protected function getValue();
    abstract protected function prefixValue($prefix);


    public function printOut() {
        echo "Hello how are you?";
    }
}

$obj=new AbstractClass();
$obj->printOut();
//Fatal error: Cannot instantiate abstract class AbstractClass

2. Any class that contains at least one abstract method must also be abstract: Abstract class can have abstract and non-abstract methods, but it must contain at least one abstract method. If a class has at least one abstract method, then the class must be declared abstract.

Note: Traits support the use of abstract methods in order to impose requirements upon the exhibiting class.

Example below :

class Non_Abstract_Class
{
   abstract protected function getValue();

    public function printOut() {
        echo "Hello how are you?";
    }
}

$obj=new Non_Abstract_Class();
$obj->printOut();
//Fatal error: Class Non_Abstract_Class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Non_Abstract_Class::getValue)

3. An abstract method can not contain body: Methods defined as abstract simply declare the method's signature - they cannot define the implementation. But a non-abstract method can define the implementation.

abstract class AbstractClass
{
   abstract protected function getValue(){
   return "Hello how are you?";
   }

    public function printOut() {
        echo $this->getValue() . "\n";
    }
}

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

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

$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
//Fatal error: Abstract function AbstractClass::getValue() cannot contain body

4. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child :If you inherit an abstract class you have to provide implementations to all the abstract methods in it.

abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();

    // Common method
    public function printOut() {
        print $this->getValue() . "<br/>";
    }
}

class ConcreteClass1 extends AbstractClass
{
    public function printOut() {
        echo "dhairya";
    }

}
$class1 = new ConcreteClass1;
$class1->printOut();
//Fatal error: Class ConcreteClass1 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::getValue)

5. Same (or a less restricted) visibility:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.

Note that abstract method should not be private.

abstract class AbstractClass
{

    abstract public function getValue();
    abstract protected function prefixValue($prefix);

        public function printOut() {
        print $this->getValue();
    }
}

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

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."<br/>";
//Fatal error: Access level to ConcreteClass1::getValue() must be public (as in class AbstractClass)

6. Signatures of the abstract methods must match:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.

abstract class AbstractClass
{

    abstract protected function prefixName($name);

}

class ConcreteClass extends AbstractClass
{


    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"), "<br/>";
echo $class->prefixName("Pacwoman"), "<br/>";
//output: Mr. Pacman
//        Mrs. Pacwoman

7. Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.

interface MyInterface{
    public function foo();
    public function bar();
}

abstract class MyAbstract1{
    abstract public function baz();
}


abstract class MyAbstract2 extends MyAbstract1 implements MyInterface{
    public function foo(){ echo "foo"; }
    public function bar(){ echo "bar"; }
    public function baz(){ echo "baz"; }
}

class MyClass extends MyAbstract2{
}

$obj=new MyClass;
$obj->foo();
$obj->bar();
$obj->baz();
//output: foobarbaz

Note: Please note order or positioning of the classes in your code can affect the interpreter and can cause a Fatal error. So, when using multiple levels of abstraction, be careful of the positioning of the classes within the source code.

below example will cause Fatal error: Class 'horse' not found

class cart extends horse {
    public function get_breed() { return "Wood"; }
}

abstract class horse extends animal {
    public function get_breed() { return "Jersey"; }
}

abstract class animal {
    public abstract function get_breed();
}

$cart = new cart();
print($cart->get_breed());
🌐
Studytonight
studytonight.com › php › php-abstract-class-and-methods
PHP Abstract Class and Method | Studytonight
An abstract method is just the declaration, where we provide name of the method and argument, while the body part is empty. Don't worry if its too much for you to understand. We will cover all the points step by step with examples, lets start by understanding how we create an abstract class.