extends

Often you need classes with similar variables and functions to another existing class. In fact, it is good practice to define a generic class which can be used in all your projects and adapt this class for the needs of each of your specific projects. To facilitate this, classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class (this is called 'inheritance' despite the fact that nobody died) and what you add in the extended definition. It is not possible to subtract from a class, that is, to undefine any existing functions or variables. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'.

<?php
class Named_Cart extends Cart {
    var 
$owner;
  
    function 
set_owner ($name) {
        
$this->owner $name;
    }
}
?>

This defines a class Named_Cart that has all variables and functions of Cart plus an additional variable $owner and an additional function set_owner(). You create a named cart the usual way and can now set and get the carts owner. You can still use normal cart functions on named carts:

<?php
$ncart 
= new Named_Cart;    // Create a named cart
$ncart->set_owner("kris");  // Name that cart
print $ncart->owner;        // print the cart owners name
$ncart->add_item("10"1);  // (inherited functionality from cart)
?>

This is also called a "parent-child" relationship. You create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

Замечание:

Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.

Коментарии

Just one thing that may seem obvious but not mentionned in this page is that you need to include/require the file containing the parent class or else you'll get an error:

<?php
require(dirname(__FILE__).'/'.'myParent.php');
// ...
myChild extends myParent {
 
// ...
}
// ...
?>
2001-11-07 14:08:26
http://php5.kiev.ua/manual/ru/keyword.extends.html
if the class B that extends class A does not have a constuctor function (i.e. a function named B), then the constructor function of A will be used instead, you don't need to make a constructor in B just to call the constructor of A.

For example:

<?php
class A
{
  function 
A()
    {
      echo 
"HEY! I'm A!\n";

    }
}

class 
extends A
{
}

$b = new B();
?>

produces the output:
HEY! I'm A!
2001-12-11 13:31:02
http://php5.kiev.ua/manual/ru/keyword.extends.html
Just to clarify something about inheritance. The following code :

<?php
class a
{
     function 
call()
     {
         
$this->toto();
     }
     
     function 
toto()
     {
          echo(
'Toto of A');
     }
}
 
class 
extends a
{
     function 
toto()
     {
          echo(
'Toto of B');
     }
}

$b=new b;
$b->call();

?>

...will correctly display "toto of B" (that is, the function declared in the parent is correctly calling the redefined function in the child)
2002-07-18 07:42:37
http://php5.kiev.ua/manual/ru/keyword.extends.html
This prints out 'ab'.  No need to create a new instance of a, therefor both methods still exists with same name.

<?php

class {
  function 
samename(){
    echo 
'a';
  }
}

class 
extends a{
  function 
samename(){
    echo 
'b';
  }
  function 
b(){
   
a::samename();
   
b::samename();
  }
}

$test_obj = new b();
?>
2002-08-16 01:37:15
http://php5.kiev.ua/manual/ru/keyword.extends.html
[Editor's note: For an alternative to multiple inheritance, see the dynamic binding via object aggregation in the corresponding section of the manual.]

Multiple Inheritance is not supported but it is easy to emulate it:

<?php
class multipleInheritance
{
    function 
callClass($class_to_call)
    {
        return new 
$class_to_call();
    }
}

class 
A
{
    function 
insideA()
    {
        echo 
"I'm inside A!<br />";
    }
}

class 
B
{

    function 
insideB()
    {
        echo 
"I'm inside B!<br />";
    }
}

class 
extends multipleInheritance
{
    function 
insideC()
    {
       
$a parent::callClass('A');
       
$a->insideA();
       
$b parent::callClass('B');
       
$b->insideB();
    }
}

$c = new C();
$c->insideC();
?>

---
This will succesfully echo:
I'm inside A!
I'm inside B!
2002-09-27 16:36:14
http://php5.kiev.ua/manual/ru/keyword.extends.html
Just a quick note to make things more clear : while multiple inheritance is not allowed, several levels of single inheritance  ARE ALLOWED indeed. Just test this example :

<?php
class {
    var 
$name='A';

    function 
disp() {
        echo 
$this->name;
    }
}

class 
extends {
    var 
$name='B';
}

class 
extends {
    var 
$name='C';
}

$truc = new C() ;
$truc->disp(); // Will output C
?>

This is especially important to keep in mind while building a huge object hierarchy. for example :

+GenericObject
->+ Person
->->Employee
->+Computer
->->+WorkStation
->->-> PPC
->->-> Intel
->->+Server
->->->LDAPServer
->->->IntranetWebServer

.. and so on. Multiple level hierarchy relationship are possible in a tree-like structure (each child has one and only one parent, except for the root object).
2003-06-22 23:58:53
http://php5.kiev.ua/manual/ru/keyword.extends.html
Just a simple example about inheritance:

<?php
class a1{
  var 
$a=10;
  function 
a1($a){
     
$this->a=$a;
  }
}

class 
a2 extends a1{
  var 
$x=11;
  function 
a2($x,$y){
     
$this->x=$x;
     
parent::a1($y); // or a1::a1($y) or $this->a1($y)
 
}
}

class 
a3 extends a2{
  var 
$q=999;
}

$x=new a3(99,9);
echo 
$x->a,"<br>",$x->x,"<br> ",$x->q;
?>

The output will be: 


99
999
2003-07-04 11:49:36
http://php5.kiev.ua/manual/ru/keyword.extends.html
It is possible to override a method innherited from a parent class by simply re-defining the method (for those of us who enjoy using abstract classes).

<?php
class A
{
    var 
$foo;

    function 
A()
    {
       
$this->foo "asdf";
    }
   
    function 
bar()
    {
        echo 
$this->foo." : Running in A";
    }
}

class 
extends A
{
    function 
bar()
    {
        echo 
$this->foo." : Running in B";
    }
}

$myClass = new B;
$myClass->bar();
?>
2004-03-02 19:35:11
http://php5.kiev.ua/manual/ru/keyword.extends.html
Автор:
Multiple inheritence is often more trouble than it's worth.  For example, you have a class foo that inherits from both class bar and class baz.  Classes bar and baz both have a fubar() method.  When you create a foo object and call its fubar() method, which fubar() method is called: bar's or baz's?

It seems to me that using aggregate to glue one class's methods and data to another object is a bit like Ruby's fixins, but I could be wrong...

[[Editor's note:
The aggregate_* functions have been dropped, as of PHP 5
-S
]]
2004-11-19 08:48:18
http://php5.kiev.ua/manual/ru/keyword.extends.html
This may seem obvious, but check this scenario. You have a class folder:

+ class
--classA.php
--classB.php
--classC.php
--mainClass.php

Here... classA, classB, classC all extend the mainClass.

If you try to create a function that automatically includes all of the classes in a folder, normally, they are included alphabetically.

When you try to instantiate classC, for example, you will get an error:

"Cannot inherit from undefined class mainClass"

EVEN IF you instantiate the mainClass before you instantiate all of the other classes.

In other words, make sure your primary class is included before all others.
2005-03-06 19:19:58
http://php5.kiev.ua/manual/ru/keyword.extends.html
When declaring a class that relies upon another file ( because it extends the class defined in that file ), you should ALWAYS require_once() that file at the top.
This applies even when planning on looping through and including everything in the folder. Use require_once() in your loop, and at the top of the file that NEEDS the include.
2005-03-31 13:11:12
http://php5.kiev.ua/manual/ru/keyword.extends.html
Автор:
Here is a simple idea that I use when I need my abstract classes (the inherited classes) implemented before my functional classes.

<?php
   
    $_CLASSES 
array_merge (
       
glob ("classes/*/*.abstract.php"),
       
glob ("classes/*/*.class.php")
    );
   
    foreach (
$_CLASSES AS $_CLASS) {
        require (
$_CLASS);
    }
   
?>
2005-11-18 23:43:30
http://php5.kiev.ua/manual/ru/keyword.extends.html
Автор:
Just a note:  It is possible to have a class inherit from multiple other classes, but only in a one-at-a-time linear hierarchy.

So this works, and C gets A and B functions:

<?php
class {
  public function 
af() { print 'a';}
  public function 
bark() {print ' arf!';}
}
class 
extends {
  public function 
bf() { print 'b';}
}
class 
extends {
  public function 
cf() { print 'c';}
  public function 
bark() {print ' ahem...'parent::bark();}
}

$c = new C;
$c->af(); $c->bf(); $c->cf();
print 
"<br />";
$c->bark();
/**results:**/
//abc
//ahem... arf!
?>

This does NOT work:

<?php
class {
  public function 
af() { print 'a';}
  public function 
bark() {print ' arf!';}
}
class 
{
  public function 
bf() { print 'b';}
}
class 
extends B/*illegal*/ {
  public function 
cf() { print 'c';}
  public function 
bark() {print ' ahem...'parent::bark();}
}

$c = new C;
$c->af(); $c->bf(); $c->cf();
print 
"<br />";
$c->bark();
//Parse Error
?>
2005-11-26 19:48:15
http://php5.kiev.ua/manual/ru/keyword.extends.html
Автор:
If you are using a child-class. Remember to call the constructor of the parent class aswell before you start using it. Otherwise you might get different results then you expected. It is stated in this document, but I got confused by the given example. So, here is my example:

<?php
error_reporting
(E_ALL);

class 
test {
  var 
$var;

  function 
test() {
   
$this->var 3;
  }
}

class 
testing extends test {
   function 
testing() {
     
parent::test();
   }

   function 
My_test() {
     return 
$this->var;
   }
}

$p = new testing();
echo 
$p->My_test(); 
// Returns 3
2006-03-03 04:54:48
http://php5.kiev.ua/manual/ru/keyword.extends.html
Автор:
Just a quick example of how PHP will handle a parent calling a function named in both the parent and the child class.  You would think it might use the function the way it is defined in the parent, but it does use the function that is defined in the child.

<?php
class One{
    function 
showOne(){
        echo 
'Function One prints';
    }
    function 
hitFunction(){
       
$this->showOne();
    }
}
class 
Two extends One{
    function 
showOne(){
        echo 
'Function Two prints';
    }
}

$thistwo = new Two;
$thistwo->hitFunction(); //prints "Function Two prints"
?>
2010-01-10 11:46:15
http://php5.kiev.ua/manual/ru/keyword.extends.html
<?php
// what if we want to extend more then one class?

Abstract class ExtensionBridge
{
   
// array containing all the extended classes
   
private $_exts = array();
    public 
$_this;
       
    function 
__construct(){$_this $this;}
   
    public function 
addExt($object)
    {
       
$this->_exts[]=$object;
    }
   
    public function 
__get($varname)
    {
        foreach(
$this->_exts as $ext)
        {
            if(
property_exists($ext,$varname))
            return 
$ext->$varname;
        }
    }
   
    public function 
__call($method,$args)
    {
        foreach(
$this->_exts as $ext)
        {
            if(
method_exists($ext,$method))
            return 
call_user_method_array($method,$ext,$args);
        }
        throw new 
Exception("This Method {$method} doesn't exists");
    }
   
   
}

class 
Ext1{
 private 
$name="";
 private 
$id="";
 public function 
setID($id){$this->id $id;}
 public function 
setName($name){$this->name $name;}
 public function 
getID(){return $this->id;}
 public function 
getName(){return $this->name;}
}

class 
Ext2{
 private 
$address="";
 private 
$country="";
 public function 
setAddress($address){$this->address $address;}
 public function 
setCountry($country){$this->country $country;}
 public function 
getAddress(){return $this->address;}
 public function 
getCountry(){return $this->country;}
}

class 
Extender extends ExtensionBridge
{
    function 
__construct()
    {
       
parent::addExt(new Ext1());
       
parent::addExt(new Ext2());
    }
   
    public function 
__toString()
    {
        return 
$this->getName().', from: '.$this->getCountry();
    }
}

$o = new Extender();
$o->setName("fabio");
$o->setCountry("brazil");
echo 
$o;
?>
don't hesitate to email me, happy coding! ;)
2010-06-29 14:19:47
http://php5.kiev.ua/manual/ru/keyword.extends.html
One thing I figured out after a long time about extending a parent class that, if the child class does not have any construct function, it will use its parent's construct.

for example:

<?php
class Main
{
    public 
$a;
   
    public function 
__construct()
    {
        echo 
'::Parent Class initiated::';
       
       
$this -> 'we are in the parent class';
    }
}

class 
Child extends Main
{
    public function 
getA()
    {
        return 
$this -> a;
    }
}

$main = new Main();
$main -> child = new Child;

echo 
$main -> child -> getA();

//Output - ::Parent Class initiated::::Parent Class initiated::we are in the parent class
?>

However, If we have a constructor in the child class as well:

<?php
class Child extends Main
{
    public function 
__construct()
    {
       
    }
    public function 
getA()
    {
        return 
$this -> a;
    }
}
?>

Then :

<?php
$main 
= new Main();
$main -> child = new Child;

echo 
$main -> child -> getA();

// Output - ::Parent Class initiated::
?>

Note that the parent variable 'a' is not inherited by the child class if the constructor from the parent class isnt called.
This behaviour of extension made me waste a lot of my precious time, as I could not understand why some of my child classes were inheriting parent variables and some were not.
Hope this helps someone..
2010-09-14 04:33:19
http://php5.kiev.ua/manual/ru/keyword.extends.html
You don't need to include_once the parent class if it's already declared.  You do have to load the parent first and then the child.
2010-09-27 20:46:46
http://php5.kiev.ua/manual/ru/keyword.extends.html
If you have a class that extends an other and both classes have a function with the same name then visibility to both classes should be the same, else you will have a fatal error.

An other interested part in the example bellow is that if visibility of the showName method is private then $this->name() will execute the showName method on class test. If it is public or protected it will execute method showName on class extendTest.

<?php
class test {

    public function 
__construct() {
    }

    public function 
name() {
       
$this->xname('John');
    }

    private function 
showName($name) {
        echo 
'my name in test is '.$name;
    }
}

class 
extendTest extends test {

    public function 
__construct() {
       
parent::__construct();
    }

    private function 
showName($name) {
        echo 
'my name in extendTest is '.$name;
    }
}

$test = new extendTest();
$test->name();
?>

result: my name in test is John

If we change visibility of the showName method to public or protected then the result of the above will be:
my name in extendTest is John
2010-12-23 13:32:29
http://php5.kiev.ua/manual/ru/keyword.extends.html
I have found this the best way to grab a parents variables and such (that I love to set in the constructor, like standardized table names, etc.). I just call the parents constructor in the child constructor, then change variables inside the child constructor that needs to be customized for that class, or just leave it alone to make sure I understand I am using the parents constructor in the child constructor. (it's a good note and practice of mine.).

<?php
class MyParentClass
{
   
$this->table_one "my_table";
   
$this->table_two "variable_table";
}

class 
MyChildClass extends MyParentClass
{
    public function 
construct()
    {
       
parent::__construct();

       
$this->table_two "newly_defined_table";

    }
    public function 
action()
   {
     
$table1 $this->table_one;
     
$table2 $this->table_two;
 
      return 
$table1 " - " $table2;
   }
}

$myChildClass = new MyChildClass;

echo 
$myChildClass->action();

//Displays my_table - newly_defined_table
?>
2012-02-10 08:27:02
http://php5.kiev.ua/manual/ru/keyword.extends.html
You can always place the universal variables and functions into the deepest class. Then call or rewrite it in child classes. See example. 

<?php

class ClassA
{
    public 
$var;
    function 
__construct()
    {
       
$this->setVar();
       
$this->getVar();
    }

    protected function 
getVar()
    {
        echo 
$this->var;
    }

    public function 
setVar()
    {
       
$this->var 1000;
    }
}

class 
ClassAChildA extends ClassA
{

    function 
__construct()
    {
       
$this->setVar();
       
$this->getVar();
    }

    public function 
setVar()
    {
       
$this->var 1001;
    }
}

class 
ClassAChildB extends ClassA
{
    function 
__construct()
    {
       
$this->var 1002;
       
$this->getVar();
    }

    public function 
getVar()
    {
        echo 
$this->var;
    }
}
//prints 1000
$parent = new ClassA();

//prints 1001
$childA = new ClassAChildA();

//prints 1002
$childB = new ClassAChildB();
?>
2015-05-04 04:56:48
http://php5.kiev.ua/manual/ru/keyword.extends.html
extending a PHP class with an included file "delays" the compilation:

<?php
require 'MySuperclass.php';
var_dump(class_exists('MyFirstClass')); // FALSE 
class MyFirstClass extends MySuperclass {} 
   
// the "compilation" of this is delayed till execution
var_dump(class_exists('MyFirstClass')); // TRUE

class AnotherSuperclass /* nothing */}
var_dump(class_exists('MySecondClass')); 
   
// returns TRUE even if it's called "before" definition 
class MySecondClass extends AnotherSuperclass {}
?>

see also the examples at function.class-exists

discoverd this when autoinitializing some static classes
2015-10-30 18:58:38
http://php5.kiev.ua/manual/ru/keyword.extends.html

    Поддержать сайт на родительском проекте КГБ