Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crazy Confusing PHP OOP "implements" "extends" at the same time

Tags:

oop

php

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!

like image 703
nicolas Avatar asked Mar 12 '11 22:03

nicolas


1 Answers

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.

like image 58
ThiefMaster Avatar answered Oct 28 '22 04:10

ThiefMaster