phpday 2025 - Call For Papers

Abstracción de clases

PHP tiene clases, métodos y propiedades abstractas. Las clases definidas como abstractas no se pueden instanciar y cualquier clase que contiene al menos un método abstracto debe ser definida como tal. Los métodos definidos como abstractos simplemente declaran la firma del método, y si es público o protegido; no pueden definir la implementación. pero no pueden definir la implementación. Las propiedades definidas como abstractas pueden declarar un requisito para get o set, y pueden proporcionar una implementación para una, pero no ambas, operaciones.

Cuando se hereda de una clase abstracta, todos los métodos definidos como abstractos en la declaración de la clase madre deben ser definidos en la clase hija; además, estos métodos deben ser definidos con la misma (o con una menos restrictiva) visibilidad. Por ejemplo, si el método abstracto está definido como protegido, la implementación de la función debe ser definida como protegida o pública, pero nunca como privada. Por otra parte, las firmas de los métodos tienen que coincidir, es decir, la declaración de tipos y el número de argumentos requeridos deben ser los mismos. Por ejemplo, si la clase derivada define un argumento opcional y la firma del método abstracto no lo hace, no habría conflicto con la firma. Esto también se aplica a los constructores a partir de PHP 5.4. Antes de PHP 5.4, las firmas del constructor podían ser diferentes.

Al heredar de una clase abstracta, todos los métodos marcados como abstractos en la declaración de la clase padre deben ser definidos por la clase hija y seguir las reglas habituales de herencia y compatibilidad de firmas.

A partir de PHP 8.4, una clase abstracta puede declarar una propiedad abstracta, ya sea pública o protegida. Una propiedad abstracta protegida puede ser satisfecha por una propiedad que sea legible/escribible tanto desde el ámbito protegido como público.

Una propiedad abstracta puede ser satisfecha ya sea por una propiedad estándar o por una propiedad con hooks definidos, correspondientes a la operación requerida.

Ejemplo #1 Ejemplo de clase abstracta

<?php
abstract class ClaseAbstracta
{
// Forzar la extensión de clase para definir este método
abstract protected function getValor();
abstract protected function
valorPrefijo($prefijo);

// Método común
public function imprimir() {
print
$this->getValor() . "\n";
}
}

class
ClaseConcreta1 extends ClaseAbstracta
{
protected function
getValor() {
return
"ClaseConcreta1";
}

public function
valorPrefijo($prefijo) {
return
"{$prefijo}ClaseConcreta1";
}
}

class
ClaseConcreta2 extends ClaseAbstracta
{
public function
getValor() {
return
"ClaseConcreta2";
}

public function
valorPrefijo($prefijo) {
return
"{$prefijo}ClaseConcreta2";
}
}

$clase1 = new ClaseConcreta1;
$clase1->imprimir();
echo
$clase1->valorPrefijo('FOO_') ."\n";

$clase2 = new ClaseConcreta2;
$clase2->imprimir();
echo
$clase2->valorPrefijo('FOO_') ."\n";
?>

El resultado del ejemplo sería:

ClaseConcreta1
FOO_ClaseConcreta1
ClaseConcreta2
FOO_ClaseConcreta2

Ejemplo #2 Ejemplo de clase abstracta

<?php
abstract class ClaseAbstracta
{
// El método abstracto sólo necesita definir los argumentos requeridos
abstract protected function nombrePrefijo($nombre);

}

class
ClaseConcreta extends ClaseAbstracta
{

// La clase derivada puede definir parámetros opcionales que no estén en la estructura del prototipo
public function nombrePrefijo($nombre, $separador = ".") {
if (
$nombre == "Pacman") {
$prefijo = "Mr";
} elseif (
$nombre == "Pacwoman") {
$prefijo = "Mrs";
} else {
$prefijo = "";
}
return
"{$prefijo}{$separador} {$nombre}";
}
}

$clase = new ClaseConcreta;
echo
$clase->nombrePrefijo("Pacman"), "\n";
echo
$clase->nombrePrefijo("Pacwoman"), "\n";
?>

El resultado del ejemplo sería:

Mr. Pacman
Mrs. Pacwoman

Una propiedad abstracta en una clase abstracta puede proporcionar implementaciones para cualquier hook, pero debe tener declarado get o set, pero no definido (como en el ejemplo anterior).

Ejemplo #3 Ejemplo de propiedad abstracta

<?php
abstract class A
{
// This provides a default (but overridable) set implementation,
// and requires child classes to provide a get implementation.
abstract public string $foo {
get;
set { $this->foo = $value };
}
}
?>
add a note

User Contributed Notes 16 notes

up
750
ironiridis at gmail dot com
16 years ago
Just one more time, in the simplest terms possible:

An Interface is like a protocol. It doesn't designate the behavior of the object; it designates how your code tells that object to act. An interface would be like the English Language: defining an interface defines how your code communicates with any object implementing that interface.

An interface is always an agreement or a promise. When a class says "I implement interface Y", it is saying "I promise to have the same public methods that any object with interface Y has".

On the other hand, an Abstract Class is like a partially built class. It is much like a document with blanks to fill in. It might be using English, but that isn't as important as the fact that some of the document is already written.

An abstract class is the foundation for another object. When a class says "I extend abstract class Y", it is saying "I use some methods or properties already defined in this other class named Y".

So, consider the following PHP:
<?php
class X implements Y { } // this is saying that "X" agrees to speak language "Y" with your code.

class X extends Y { } // this is saying that "X" is going to complete the partial class "Y".
?>

You would have your class implement a particular interface if you were distributing a class to be used by other people. The interface is an agreement to have a specific set of public methods for your class.

You would have your class extend an abstract class if you (or someone else) wrote a class that already had some methods written that you want to use in your new class.

These concepts, while easy to confuse, are specifically different and distinct. For all intents and purposes, if you're the only user of any of your classes, you don't need to implement interfaces.
up
293
mbajoras at gmail dot com
14 years ago
Here's an example that helped me with understanding abstract classes. It's just a very simple way of explaining it (in my opinion). Lets say we have the following code:

<?php
class Fruit {
private
$color;

public function
eat() {
//chew
}

public function
setColor($c) {
$this->color = $c;
}
}

class
Apple extends Fruit {
public function
eat() {
//chew until core
}
}

class
Orange extends Fruit {
public function
eat() {
//peel
//chew
}
}
?>

Now I give you an apple and you eat it.

<?php
$apple
= new Apple();
$apple->eat();
?>

What does it taste like? It tastes like an apple. Now I give you a fruit.

<?php
$fruit
= new Fruit();
$fruit->eat();
?>

What does that taste like??? Well, it doesn't make much sense, so you shouldn't be able to do that. This is accomplished by making the Fruit class abstract as well as the eat method inside of it.

<?php
abstract class Fruit {
private
$color;

abstract public function
eat();

public function
setColor($c) {
$this->color = $c;
}
}
?>

Now just think about a Database class where MySQL and PostgreSQL extend it. Also, a note. An abstract class is just like an interface, but you can define methods in an abstract class whereas in an interface they are all abstract.
up
5
shewa12kpi at gmail dot com
3 years ago
<?php
//Here is a good example of abstract class. Here BaseEmployee is not actual employee its just asbtract class that reduce our code and enforce child class to implement abstract method

abstract class BaseEmployee {

/**
* employee common attributes could in asbtract class
*/
public $firstname,
$lastname;

function
__construct($fn, $ln){
$this->firstname = $fn;
$this->lastname = $ln;
}

public function
getFullName() {

return
"$this->firstname $this->lastname";
}

/**
* asbtract method that a child class must have to define
*/
abstract protected static function task();
}

class
WebDeveloper extends BaseEmployee {

static function
task()
{
return
' Develop web application';
}
}

class
HR extends BaseEmployee {

static function
task()
{
return
' Manage human resources';
}

}
/**
* now instantiate and get data
*/
$webDeveloper = new WebDeveloper('shaikh','ahmed');
echo
$webDeveloper->getFullName();
echo
$webDeveloper->task();
up
29
jai at shaped dot ca
7 years ago
This example will hopefully help you see how abstract works, how interfaces work, and how they can work together. This example will also work/compile on PHP7, the others were typed live in the form and may work but the last one was made/tested for real:

<?php

const = PHP_EOL;

// Define things a product *has* to be able to do (has to implement)
interface productInterface {
public function
doSell();
public function
doBuy();
}

// Define our default abstraction
abstract class defaultProductAbstraction implements productInterface {
private
$_bought = false;
private
$_sold = false;
abstract public function
doMore();
public function
doSell() {
/* the default implementation */
$this->_sold = true;
echo
"defaultProductAbstraction doSell: {$this->_sold}".;
}
public function
doBuy() {
$this->_bought = true;
echo
"defaultProductAbstraction doBuy: {$this->_bought}".;
}
}

class
defaultProductImplementation extends defaultProductAbstraction {
public function
doMore() {
echo
"defaultProductImplementation doMore()".;
}
}

class
myProductImplementation extends defaultProductAbstraction {
public function
doMore() {
echo
"myProductImplementation doMore() does more!".;
}
public function
doBuy() {
echo
"myProductImplementation's doBuy() and also my parent's dubai()".;
parent::doBuy();
}
}

class
myProduct extends defaultProductImplementation {
private
$_bought=true;
public function
__construct() {
var_dump($this->_bought);
}
public function
doBuy () {
/* non-default doBuy implementation */
$this->_bought = true;
echo
"myProduct overrides the defaultProductImplementation's doBuy() here {$this->_bought}".;
}
}

class
myOtherProduct extends myProductImplementation {
public function
doBuy() {
echo
"myOtherProduct overrides myProductImplementations doBuy() here but still calls parent too".;
parent::doBuy();
}
}

echo
"new myProduct()".;
$product = new myProduct();

$product->doBuy();
$product->doSell();
$product->doMore();

echo
."new defaultProductImplementation()".;

$newProduct = new defaultProductImplementation();
$newProduct->doBuy();
$newProduct->doSell();
$newProduct->doMore();

echo
."new myProductImplementation".;
$lastProduct = new myProductImplementation();
$lastProduct->doBuy();
$lastProduct->doSell();
$lastProduct->doMore();

echo
."new myOtherProduct".;
$anotherNewProduct = new myOtherProduct();
$anotherNewProduct->doBuy();
$anotherNewProduct->doSell();
$anotherNewProduct->doMore();
?>

Will result in:
<?php
/*
new myProduct()
bool(true)
myProduct overrides the defaultProductImplementation's doBuy() here 1
defaultProductAbstraction doSell: 1
defaultProductImplementation doMore()

new defaultProductImplementation()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
defaultProductImplementation doMore()

new myProductImplementation
myProductImplementation's doBuy() and also my parent's dubai()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
myProductImplementation doMore() does more!

new myOtherProduct
myOtherProduct overrides myProductImplementations doBuy() here but still calls parent too
myProductImplementation's doBuy() and also my parent's dubai()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
myProductImplementation doMore() does more!

*/
?>
up
66
a dot tsiaparas at watergate dot gr
13 years ago
Abstraction and interfaces are two very different tools. The are as close as hammers and drills. Abstract classes may have implemented methods, whereas interfaces have no implementation in themselves.

Abstract classes that declare all their methods as abstract are not interfaces with different names. One can implement multiple interfaces, but not extend multiple classes (or abstract classes).

The use of abstraction vs interfaces is problem specific and the choice is made during the design of software, not its implementation. In the same project you may as well offer an interface and a base (probably abstract) class as a reference that implements the interface. Why would you do that?

Let us assume that we want to build a system that calls different services, which in turn have actions. Normally, we could offer a method called execute that accepts the name of the action as a parameter and executes the action.

We want to make sure that classes can actually define their own ways of executing actions. So we create an interface IService that has the execute method. Well, in most of your cases, you will be copying and pasting the exact same code for execute.

We can create a reference implemention for a class named Service and implement the execute method. So, no more copying and pasting for your other classes! But what if you want to extend MySLLi?? You can implement the interface (copy-paste probably), and there you are, again with a service. Abstraction can be included in the class for initialisation code, which cannot be predefined for every class that you will write.

Hope this is not too mind-boggling and helps someone. Cheers,
Alexios Tsiaparas
up
12
swashata4u at gmail dot com
6 years ago
Here is another thing about abstract class and interface.

Sometimes, we define an interface for a `Factory` and ease out some common methods of the `Factory` through an `abstract` class.

In this case, the abstract class implements the interface, but does not need to implement all methods of the interface.

The simple reason is, any class implementing an interface, needs to either implement all methods, or declare itself abstract.

Because of this, the following code is perfectly ok.

<?php
interface Element {
/**
* Constructor function. Must pass existing config, or leave as
* is for new element, where the default will be used instead.
*
* @param array $config Element configuration.
*/
public function __construct( $config = [] );

/**
* Get the definition of the Element.
*
* @return array An array with 'title', 'description' and 'type'
*/
public static function get_definition();

/**
* Get Element config variable.
*
* @return array Associative array of Element Config.
*/
public function get_config();

/**
* Set Element config variable.
*
* @param array $config New configuration variable.
*
* @return void
*/
public function set_config( $config );
}

abstract class
Base implements Element {

/**
* Element configuration variable
*
* @var array
*/
protected $config = [];

/**
* Get Element config variable.
*
* @return array Associative array of Element Config.
*/
public function get_config() {
return
$this->config;
}

/**
* Create an eForm Element instance
*
* @param array $config Element config.
*/
public function __construct( $config = [] ) {
$this->set_config( $config );
}
}

class
MyElement extends Base {

public static function
get_definition() {
return [
'type' => 'MyElement',
];
}

public function
set_config( $config ) {
// Do something here
$this->config = $config;
}
}

$element = new MyElement( [
'foo' => 'bar',
] );

print_r( $element->get_config() );
?>

You can see the tests being executed here and PHP 5.4 upward, the output is consistent. https://3v4l.org/8NqqW
up
19
shaman_master at list dot ru
6 years ago
Also you may set return/arguments type declaring for abstract methods (PHP>=7.0)
<?php
declare(strict_types=1);

abstract class
Adapter
{
protected
$name;
abstract public function
getName(): string;
abstract public function
setName(string $value);
}

class
AdapterFoo extends Adapter
{
public function
getName(): string
{
return
$this->name;
}
// return type declaring not defined in abstract class, set here
public function setName(string $value): self
{
$this->name = $value;
return
$this;
}
}
?>
up
8
Eugeny at Kostanay dot KZ
8 years ago
A snippet of code to help you understand a bit more about properties inside abstract classes:
<?php
abstract class anotherAbsClass
{
// Define and set a static property
static $stProp = 'qwerty'; // We can still use it directly by the static way
// Define and set a protected property
protected $prProp = 'walrus';
// It is useless to set any other level of visibility for non-static variables of an abstract class.
// We cannot access to a private property even inside a declared method of an abstract class because we cannot call that method in the object context.
// Implementation of a common method
protected function callMe() {
echo
'On call: ' . $this->prProp . PHP_EOL;
}
// Declaration of some abstract methods
abstract protected function abc($arg1, $arg2);
abstract public function
getJunk($arg1, $arg2, $arg3, $junkCollector = true);
// Note: we cannot omit an optional value without getting error if it has already been declared by an abstract class
}

class
someChildClass extends anotherAbsClass
{
function
__construct() {
echo
$this->callMe() . PHP_EOL; // now we get the protected property $prProp inhereted from within the abstract class
}
// There must be implementation of the declared functions abc and getJunk below
protected function abc($val1, $val) {
// do something
}
function
getJunk($val1, $val2, $val3, $b = false) { // optional value is neccessary, because it has been declared above
// do something
}
}

echo
anotherAbsClass::$stProp; // qwerty
$objTest = new someChildClass; // On call: walrus
?>
up
69
joelhy
13 years ago
The documentation says: "It is not allowed to create an instance of a class that has been defined as abstract.". It only means you cannot initialize an object from an abstract class. Invoking static method of abstract class is still feasible. For example:
<?php
abstract class Foo
{
static function
bar()
{
echo
"test\n";
}
}

Foo::bar();
?>
up
20
sneakyimp at hotmail dot com
17 years ago
Ok...the docs are a bit vague when it comes to an abstract class extending another abstract class. An abstract class that extends another abstract class doesn't need to define the abstract methods from the parent class. In other words, this causes an error:

<?php
abstract class class1 {
abstract public function
someFunc();
}
abstract class
class2 extends class1 {
abstract public function
someFunc();
}
?>

Error: Fatal error: Can't inherit abstract function class1::someFunc() (previously declared abstract in class2) in /home/sneakyimp/public/chump.php on line 7

However this does not:

<?php
abstract class class1 {
abstract public function
someFunc();
}
abstract class
class2 extends class1 {
}
?>

An abstract class that extends an abstract class can pass the buck to its child classes when it comes to implementing the abstract methods of its parent abstract class.
up
19
bishop
14 years ago
Incidentally, abstract classes do not need to be base classes:

<?php
class Foo {
public function
sneeze() { echo 'achoooo'; }
}

abstract class
Bar extends Foo {
public abstract function
hiccup();
}

class
Baz extends Bar {
public function
hiccup() { echo 'hiccup!'; }
}

$baz = new Baz();
$baz->sneeze();
$baz->hiccup();
?>
up
8
jai at shaped dot ca
7 years ago
An interface specifies what methods a class must implement, so that anything using that class that expects it to adhere to that interface will work.

eg: I expect any $database to have ->doQuery(), so any class I assign to the database interface should implement the databaseInterface interface which forces implementation of a doQuery method.

<?php
interface dbInterface {
public function
doQuery();
}

class
myDB implements dbInterface {
public function
doQuery() {
/* implementation details here */
}
}

$myDBObj = new myDB()->doQuery();
?>

An abstract class is similar except that some methods can be predefined. Ones listed as abstract will have to be defined as if the abstract class were an interface.

eg. I expect my $person to be able to ->doWalk(), most people walk fine with two feet, but some people have to hop along :(

<?php
interface PersonInterface() {
/* every person should walk, or attempt to */
public function doWalk($place);
/* every person should be able to age */
public function doAge();
}

abstract class
AveragePerson implements PersonInterface() {
private
$_age = 0;
public function
doAge() {
$this->_age = $this->_age+1;
}
public function
doWalk($place) {
echo
"I am going to walk to $place".PHP_EOL;
}
/* every person talks differently! */
abstract function talk($say);
}

class
Joe extends AveragePerson {
public function
talk($say) {
echo
"In an Austrailian accent, Joe says: $say".PHP_EOL;
}
}

class
Bob extends AveragePerson {
public function
talk($say) {
echo
"In a Canadian accent, Bob says: $say".PHP_EOL;
}
public function
doWalk($place) {
echo
"Bob only has one leg and has to hop to $place".PHP_EOL;
}
}

$people[] = new Bob();
$people[] = new Joe();

foreach (
$people as $person) {
$person->doWalk('over there');
$person->talk('PHP rules');
}
?>
up
7
joebert
17 years ago
I don't agree with jfkallens' last comparison between Abstract Classes & Object Interfaces completely.

In an Abstract Class, you can define how some methods work, where as in an Object Interface you can not.

An Object Interface is essentually nothing but a list of function names that a class must define if the class implements that interface.

An Abstract Class is essentually a prototype which hints towards what extending classes should be doing.
An Abstract Class can also be thought of as a Base Class that provides some basic functionality, & also defines a built-in Object Interface that all extending classes will implement.

So, an Object Interface is really a built-in part of an Abstract Class.
up
3
Malcolm
8 years ago
I've found an inconsistency with: Example #2 Abstract class example

If you remove the default value of $separator

<?php
public function prefixName($name, $separator) {
// ...
}
?>

Then php will show this fatal message:
Fatal error: Declaration of ConcreteClass::prefixName() must be compatible with AbstractClass::prefixName($name) in /index.php on line 23

Stange enough it gives an incorrect declaration of "ConcreteClass::prefixName()"... It is missing both arguments. Because of that I'm assuming that this is just a bug that maybe already has been taking care of in newer versions. (Or is just specific to my version) I'm mainly noting this because it was driving me absolutely insane in some test code that I was writing derived from Example #2 (without a default value for an extra argument). Perhaps this saves some frustrations to other people.

--
Please note that i'm running this on php5.5.
OS: ubuntu-16.04-server-amd64.iso
Repo: ppa:ondrej/php

# php5.5 --version
PHP 5.5.36-2+donate.sury.org~xenial+1 (cli)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
up
2
arma99eDAN at yahoo dot com
9 years ago
You can use an abstract class like this too:

abstract class A{
public function show(){
echo 'A';
}
}
class B extends A{
public function hello(){
echo 'B';
parent::show();
}
}

$obj = new B;
$obj->hello(); // BA
# See that the abstract class does not have at least one abstract method
# Even in this case, I'm still able to extend it, or call its non-abstract member
up
0
designbyjeeba at gmail dot com
13 years ago
Please be aware of the visibility of the parent fields. If the fields are private, then you are not going to see those fields in their childrens. Its basic OOP, but can be problematic sometimes.
To Top