10
abstract class SF_Model_Acl_Abstract 
    extends SF_Model_Abstract
    implements SF_Model_Acl_Interface, Zend_Acl_Resource_Interface
{
    protected $_acl;
    protected $_identity;
    public function setIdentity($identity)
    {
    if (is_array($identity)) {
        ......
        ......

Can you help me explain how it can "implements" "extends" at the same time?
Does it just combine the 3 class together?

I am totally confused!

5
  • It's basically the same. But since PHP follows a more limited OOP paradigm it doesn't support multiple inheritance. Hence it uses empty class signatures (=interfaces) and supports only one real inheritance (=extends class).
    – mario
    Commented Mar 12, 2011 at 22:32
  • then Java also follows a more limited OOP ;) - kudos PHP you are not behind
    – EGL 2-101
    Commented Mar 12, 2011 at 22:34
  • Multiple inheritance is considered a bad practice, as it leads to really difficult code, and we all know: Good things are simple. The world can live without it, experience has proven it.
    – Falcon
    Commented Mar 12, 2011 at 22:58
  • @Falcon: I don't want to start an irrelevant discussion here, but the "bad practice" part is subjective, so is the simplicity of interfaces vs. proper MI, and the world can live without OOP.
    – aib
    Commented Mar 12, 2011 at 23:09
  • @Falcon: It might be a bad feature in statically compiled languages due to function precedence (see diamond problem), but works pretty well in scripting languages (see Python). c2.com/cgi/wiki?MultipleInheritanceIsNotEvil - The planned traits feature in PHP 5.4 adds significantly more syntactic and semantic complexity than it tries to solve with eschewing MI because of some overgeneralized meme.
    – mario
    Commented Mar 12, 2011 at 23:11

5 Answers 5

17

extends is for inheritance, i.e. inheriting the methods/fields from the class. A PHP class can only inherit from one class.

implements is for implementing interfaces. It simply requires the class to have the methods which are defined in the implemented interfaces.

Example:

interface INamed { function getName($firstName); }
class NameGetter { public function getName($firstName) {} }
class Named implements INamed { function getName($firstName) {} }
class AlsoNamed extends NameGetter implements INamed {}
class IncorrectlyNamed implements INamed { function getName() {} }
class AlsoIncorrectlyNamed implements INamed { function setName($newName) {} }

This code throws a fatal error in line 5 as a method from the interface is not properly implemented (argument missing). It would also throw a fatal error in line 6 as the method from the interface is not implemented at all.

2
  • Thanks, man~ I feel much better now...I guess the trick is to make a distinction that extends is for inheritances, while implements just for making the interface functions.
    – nicolas
    Commented Mar 13, 2011 at 1:59
  • 1
    I just had the same problem at work.. extending 1 class and implementing another 3! Anyways it's always helpful to check out the PHP manual on inheritance and interfacing
    – xyhhx
    Commented Nov 9, 2012 at 17:04
5

Yes, PHP can implement multiple interface using implements, but it can inherit only one class using extends

5

Implements and extends are two different kinds of shoes.

Extends tells the compiler/interpreter that the class is derived from an other class. Implements tells the compiler/interpreter, that the class must implement a contract, defined in an interface.

Look up interfaces, as they are the backbone of polymorphy in OOP. Extends basically implements the public (and semi public, protected) interface of the super class automatically, as you derive from it.

2

extends: can use and/or override any parent's method.

implements: must have all the interface methods: every interface's method must be at least declared in the class that implements.

1

It just implements interfaces, which describe which methods are required, so other methods have a defined interface to work against, see http://php.net/manual/en/language.oop5.interfaces.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.