文字

抽象类

PHP 5 支持抽象类和抽象方法。定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。

继承一个抽象类的时候,子类必须定义父类中的所有抽象方法;另外,这些方法的访问控制必须和父类中一样(或者更为宽松)。例如某个抽象方法被声明为受保护的,那么子类中实现的方法就应该声明为受保护的或者公有的,而不能定义为私有的。此外方法的调用方式必须匹配,即类型和所需参数数量必须一致。例如,子类定义了一个可选参数,而父类抽象方法的声明里没有,则两者的声明并无冲突。 这也适用于 PHP 5.4 起的构造函数。在 PHP 5.4 之前的构造函数声明可以不一样的。

Example #1 抽象类示例

<?php
abstract class  AbstractClass
{
 
// 强制要求子类定义这些方法
    
abstract protected function  getValue ();
    abstract protected function 
prefixValue ( $prefix );

    
// 普通方法(非抽象方法)
    
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
{
    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" ;
?>

以上例程会输出:

ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2

Example #2 抽象类示例

<?php
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" ),  "\n" ;
echo 
$class -> prefixName ( "Pacwoman" ),  "\n" ;
?>

以上例程会输出:

Mr. Pacman
Mrs. Pacwoman

老代码中如果没有自定义类或函数被命名为“abstract”,则应该能不加修改地正常运行。

用户评论:

[#1] rmoisto at gmail dot com [2015-09-26 15:26:46]

The self keyword in an abstract class will refer to the abstract class itself, not the extending class no matter what.

For instance the following code looks really pretty, yet results in a Fatal error (Cannot instantiate abstract class Basic).

<?php
abstract class Basic {
    public static function 
doWork() {
        return (new 
self())->work();
    }

    abstract protected function 
work();
}

class 
Keeks extends Basic {
    protected function 
work() {
        return 
'Keeks';
    }
}

echo 
Keeks::doWork();
?>

[#2] arma99eDAN at yahoo dot com [2015-04-08 08:10:52]

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

[#3] aloydev2586 at gmail dot com [2014-09-25 09:33:12]

Just in case you are confused about function arguments: 



abstract class my_class {
     abstract public function my_function($number);
}

class subclass extends my_class {
     public function my_function($new_number, $string = ' is an integer!!!')
     {
          echo $new_number . $string;
     }
}
$var = new subclass();
$var->my_function(1024); //this will output: 1024 is an integer!!!


abstract class my_class {
     abstract public function my_function($number);
}

class subclass extends my_class {
    //now $string = ' is a float!!!'
     public function my_function($new_number, $string = ' is a float!!!')
     {
          echo $new_number . $string;
     }

}
$var = new subclass();
//added ' is an integer'
$var->my_function(1024, ' is an integer!!!'); //this will output: 1024 is an integer!!!, rewrote $string.



abstract class my_class {
     abstract public function my_function($number);
}

class subclass extends my_class {
     //now $string isn't initialized
     public function my_function($new_number, $string )
     {
          echo $new_number . $string;
     }

}

$var = new subclass();
$var->my_function(1024, ' is an integer!!!'); 



abstract class my_class {
     abstract public function my_function($number);
}

class subclass extends my_class {    
     public function my_function($new_number, $string )
     {
          echo $new_number . $string;
     }

}
$var = new subclass();
//no second argument, no matter
$var->my_function(1024);//fatal error  too. Optional arguments have to be initialized in the extending class function.

[#4] sam at righthandedmonkey dot com [2014-09-11 15:43:52]

Please note order or positioning of the classes in your code can affect the interpreter and can cause a Fatal error: Class 'YourClass' not found if there are multiple levels of abstraction out of order.  For example:
<?php
abstract class horse extends animal {
    public function 
get_breed() { return "Jersey"; }
}

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

$cart = new cart();
print(
$cart->get_breed());
?>


this outputs:
Wood

However, if you put the cart before the abstract horse (literally):

<?php
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());
?>


this throws an error:
Fatal error: Class 'horse' not found

So, when using multiple levels of abstraction, be careful of the positioning of the classes within the source code - and don't put the cart before the abstract horse.

[#5] nikola at petkanski dot com [2014-02-10 18:33:25]

Invoking static method of abstract class should be removed.

What interfaces are?
- A mean to ensure all implementation have the same methods implemented.

What is an abstract class?
- It is a interface that can also include some concrete methods.

Is it right for the developer to be able to invoke a static method of an interface?
- I think not.

The GoF teach us to rely on abstract classes and interfaces to hide differences between subclasses from clients.
- Interface defines an object??s use (protocol)
- Implementation defines particular policy

I don't think one should be able to call some abstract logic that is defined inside an abstract class, without even inheriting the class itself.

[#6] passerbyxp at gmail dot com [2013-01-08 10:56:25]

It's not quite clear in this document, so in case someone wonder, abstract method can be static:

<?php
abstract class FOO
{
    abstract static function 
dump();
}
class 
BAR extends FOO
{
    static function 
dump(){var_dump("BAR");}
}
BAR::dump(); // string(3) "BAR"

class FAULT extends FOO
{
    function 
pr(){var_export("WTF");}
// FATAL error
?>

[#7] oliver at ananit dot de [2011-11-27 08:07:25]

Abstract classes may have an final constructor, and sometime it makes sense to implement a class with a final constructor.

<?php
abstract class AbstractModel
{
    
//our models must use the default constuctor
        
public final function __construct(){}
    public function 
inject($array){
        foreach(
array_keys(get_class_vars(get_called_class()))  as $property){
            
$this->$property $array[$property];
        }
    }
}

class 
ProductModel extends AbstractModel
{
    public 
$name;
    public 
$price;
    protected 
$id;
    
    public function 
getId(){return $this->id;}
}

class 
Factory{
    private 
$dataSource;
    public function 
__consruct($dataSource){
        
$this->dataSource $dataSource;
    }
    
    public function 
get($class$table$filter$orderby$limit){
        
$result = array();
        foreach(
$datasource->fetchAssoc($table$filter$orderby$limit) as $rawData){
            
$obj = new $class();//this can only work if ALL models have a default constructor
            
$obj->inject($rawData);
            
$result[] = $obj;
        }
        return 
$result;
    }
}
?>


Note: This is a very simple example, and I am aware that there are other (better) ways to do this.
Oliver Anan

[#8] joelhy [2011-05-31 19:27:48]

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();
?>

[#9] a dot tsiaparas at watergate dot gr [2011-03-26 15:57:02]

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

[#10] designbyjeeba at gmail dot com [2010-12-26 06:29:22]

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.

[#11] bishop [2010-07-28 10:36:02]

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();
?>

[#12] mbajoras at gmail dot com [2009-12-31 23:59:14]

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.

[#13] nathan dot vorbei dot tech at gmail dot com [2009-11-16 22:55:50]

"additionally, these methods must be defined with the same (or a less restricted) visibility."

The words were not restricted in abstract class but also normal classes,
the method in child Class which overwrites the parent Class can also change the the visibility of the method to same or less restricted.
for example:
<?php
class ClassOne {
    protected static $staticone = 'nathan';
    protected function changestaticone() {
        return self::$staticone = 'john';
    }
}

class ClassTwo extends ClassOne {
    public function changestaticone() {
        return self::$staticone = 'Alexey';
    }

$classtwo = new ClassTwo();
echo $classtwo->changestaticone();

[#14] pete at surfaceeffect dot com [2009-10-13 02:49:34]

One fairly important difference between php's abstract functions and, say, Java, is that php does not specify the return type in any way - or indeed whether there has to be one.

<?php public abstract function square($number); ?>

could be implemented by...

<?php
public function square($number) {
   return 
$number*$number;
}
?>


or 

<?php
public function square($number) {
   print (
$number*$number);
}
?>


So you need to take care that incompatibilities don't arise due to not returning the right kind of value and this is not enforced in any way.

[#15] Cheese Doodle [2009-09-22 16:13:37]

There isn't really that much of a great hurdle in understanding these things, there really isn't. 

If you're defining a new class that is abstract, it means that you can make some non-abstract functions that you can use to define the general underlying behavior of that class along side abstract ones. 

In interfaces, you can't do that since functions defined therewithin cannot have a body.

Abstract functions you use for classes that must define more specific behavior when "extending" your class. 

So for a crude example - define by your non-abstract functions how that particular object (which may be part of a larger class hierarchy) would store and process it's data in SQL, XML, etc.

Then define abstract functions which allow someone implementing that class to specifically manipulate the data that is to be stored. Then require a format which this data must be returned in, and then in your non-abstract functions call those functions on destruction, in normal runtime, and so on. 

Again, non-abstract functions, or even another class could implement the finer points of ensuring that data is in the correct format, and so on, ad infinitum. 

It isn't too much of a reach to say that if you used a normal class instead of an abstract class, then there isn't much intrinsic requirement between the two classes. 

Assuming that you wanted the functions to use each-others functions and you'd need to use them specifically by name, you'd have to write some code which checked to see -- lamely using function_exists() and other lamery -- if that class has the function you require for interoperability, when you could avoid all possible confusion and headaches by simply using the right tool for the job.

And reading a decent OOP book.

[#16] eeescalona [2008-06-02 21:04:28]

here is a real world example of abstract using:

a (abstract) person class
a student and an employee final class, which extends person class.

simple theory is that both student and employee is an extension of the person class.  the difference lies on which table the data is written on, and what other pre processing (ie mandatory field checking, type checking, etc.) needed before writing each of the classes.

codes:

<?php

abstract class person {
    
    abstract protected function 
write_info();
    
    public 
$LastName;
    public 
$FirstName;
    public 
$BirthDate;
    
    public function 
get_Age($today=NULL){
        
//age computation function
    
}
}

final class 
employee extends person{
    public 
$EmployeeNumber;
    public 
$DateHired;

    public function 
write_info(){
        echo 
"Writing "$this->LastName "'s info to emloyee dbase table";    
        
//ADD unique mandatory checking unique to EMPLOYEE ONLY
        //actual sql codes here
    
}
}

final class 
student extends person{
    public 
$StudentNumber;
    public 
$CourseName;
    
    public function 
write_info(){
        echo 
"Writing "$this->LastName "'s info to student dbase table";
        
//ADD unique mandatory checking unique to STUDENT ONLY
        //actual sql codes here
    
}
}

///----------
$personA = new employee;
$personB = new student;

$personA->FirstName="Joe";
$personA->LastName="Sbody";

$personB->FirstName="Ben";
$personB->LastName="Dover";

$personA->write_info();
?>


OUTPUT:Writing Sbody's info to emloyee dbase table

[#17] ironiridis at gmail dot com [2008-03-27 13:56:30]

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 implements { } // this is saying that "X" agrees to speak language "Y" with your code.

class extends { } // 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.

[#18] sneakyimp at hotmail dot com [2007-10-09 17:05:57]

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.

[#19] joebert [2007-06-24 04:09:17]

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.

上一篇: 下一篇: