PHP Doku:: parent - keyword.parent.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchAppendicesClasses and Objects (PHP 4)parent

Ein Service von Reinhard Neidl - Webprogrammierung.

Classes and Objects (PHP 4)

<<Scope Resolution Operator (::)

Serializing objects - objects in sessions>>

parent

You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class.

Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class. By doing this, you avoid using the name of your base class in more than one place. Should your inheritance tree change during implementation, the change is easily made by simply changing the extends declaration of your class.

<?php
class {
    function 
example() {
        echo 
"I am A::example() and provide basic functionality.<br />\n";
    }
}

class 
extends {
    function 
example() {
        echo 
"I am B::example() and provide additional functionality.<br />\n";
        
parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>

30 BenutzerBeiträge:
- Beiträge aktualisieren...
pjm at pehjota dot com
21.10.2009 7:32
I've seen people in the comments below use this, but I've also seen some unecessary workarounds to achieve it. So I thought I'd clarify, even though it is shown in the documentation. You CAN reference non-static parent methods, as in other languages. For example...

<?php

abstract class ParentClass {
    public function
doSomething() {
        echo
"done.\n";
    }
}

class
ChildClass extends ParentClass {
    public function
doSomething() {
        echo
"attempting something.\n";
       
parent::doSomething();
    }
}

$obj = new ChildClass();
$obj->doSomething();

?>

This will output both "attempting something." and "done." As you might expect, it also works if the parent method is protected. Users of other languages may have tried defining static and non-static methods with the same name. This is why that doesn't work ("parent" can refer to both static and non-static methods, and there's no way to differentiate them).

However, you CANNOT use the "parent" keyword to reference the parent's variables. When in a class, "parent" will always look for static variables. For example, this results in an "Access to undeclared static property" fatal error:

<?php

abstract class ParentClass {
    public
$var = 5;
}

class
ChildClass extends ParentClass {
    public function
setVar($val) {
       
parent::$var = $val;
    }
    public function
getVar() {
        return
parent::$var;
    }
}

$obj = new ChildClass();
$obj->setVar(6);  // Results in fatal error.
echo $obj->getVar() . "\n"// Results in fatal error.
echo $obj->var . "\n"// Would work and reference the parent's $var unless the child has its own $var.

?>
lesion at autistici dot org
5.06.2009 12:27
take care of using parent::method with not declared method when using __call because in this case the string method will be lowercase:

<?php
class A {
    function
__call( $method, $args ) {
      echo
get_class( $this )  . ' ' . $method . "\n";
    }
}

class
B extends A {
    function
getTest() {
       
parent::getTest();
    }
}

$a = new A();
$a->getTest();
$b = new B();
$b->getTest();

?>

output:

A getTest
B gettest
php at stock-consulting dot com
10.10.2008 18:40
It appears that parent refers to the base class of the subclass in which the method is defined, not the subclass in which the method is exceuted. This behaviour is consistent with other object-oriented languages. PHP's documentation is a bit unclear here, so I had to test it (PHP5.2.6).
nate @ 8
10.08.2008 20:29
in below example need to use

$this->b->par =& $this;

to ensure future changes to the parent are also referenced
nate at 8
3.08.2008 6:06
To simulate traditional C# like parent access, child classes can be defined in the parent's constructor and linked via explict definition. ($this->b->par = $this;)

<?php
class A {

  var
$foo;
  var
$b;

  function
A(){
   
$this->foo = "foo!";
   
$this->b = new B;
   
$this->b->par = $this;
    }
   
  function
bar(){
    print(
"bar!");
    }
   
};

class
B {

  var
$par;

  function
B(){
    }
   
  function
bar(){
    print(
$this->par->foo );
   
$this->par->bar();
    }
   
};

$test = new A;
$test->b->bar();
?>

This returns:

foo!bar!

demonstrating both variable and function access via the parent.

10.03.2007 12:49
<?php
   
class A {
      function
X() {
        echo
'y';
      }
    }
   
    class
B extends A{
      function
X() {
        echo
'z';
      }
     
      function
Y() {
        echo
parent::X();
      }
    }
   
    class
C extends B {
      function
Z () {
        echo
parent::Y();
      }
    }

$c = new C();
$c->Z();                  //output is 'y'
?>
KOmaSHOOTER at gmx dot de
20.12.2004 2:59
An example for using a memberfunction with another memberfunction.

The sequencing for the creating of the class is important.

<?php
class A
{
   function
example()
   {
       echo
"I am A::example() and provide basic functionality.<br>\n";
   }
}

class
B extends A
{
   function
example()
   {
       echo
"I am B::example() and provide additional functionality.<br>\n";
      
parent::example();
   }
}

class
C extends A
{
  
   function
example()
   {
          global
$b;
       echo
"I am c::example() and provide additional functionality.<br>\n";
      
parent::example();
      
$b->example();
   }
}
$b = new B();
$c = new C();
$c->example();
;
?>

Result:

I am c::example() and provide additional functionality.
I am A::example() and provide basic functionality.
I am B::example() and provide additional functionality.
I am A::example() and provide basic functionality.

27.05.2004 19:40
About the constructors :

Yes, a good pratrice could be to use a method called by the real constructor. In PHP 5, constructor are unified with the special __construct method. So we could do :

<?php
class A {
    var
$a = "null";
    function
A() {
       
$args = func_get_args();
       
call_user_func_array(array(&$this, "__construct"), $args);
    }
    function
__construct($a) {
       
$this->a = $a;
        echo
"A <br/>";
    }
}

class
B extends A {
    var
$b = "null";

    function
__construct($a, $b) {
       
$this->b = $b;
        echo
"B <br/>";
       
parent::__construct($a);
    }
    function
display() {
        echo
$this->a, " / ", $this->b, "<br/>";
    }
}

$b = new B("haha", "bobo");
$b->display();

?>

It will have the same behavior than standard php constructors, except that you can not pass arguments by reference.
andyfaeglasgowTAKE_ME_OUT at yahoo dot co dot uk
14.05.2004 5:15
re: referring to a parents variables

The documentation doesn't say anything about how one should refer to the variables of a parent class in a child class (maybe it's obvious but I thought I'd point it out anyway).

class Actress {
    var $bust;
   
    function Actress() {$this->bust = "32C";}
    function show() {echo "Bust: $this->bust...";}
}

class HollywoodActress extends Actress{
    var $attitude;
   
    function HollywoodActress () {
        $this->Actress();
        $this->attitude = "Fiery";
    }
   
    function show() {
        parent::show();
        echo "Attitude: $this->attitude";
    }
   
    function modify_bust() {
        $this->bust = "32DD";
    }
}

$girl_at_theatre = new Actress();
$girl_at_theatre->show();
//output: "Bust: 32C..."

$object_of_gossip = new HollywoodActress();
$object_of_gossip->modify_bust();
$object_of_gossip->show();
//output: "Bust: 32D:...Attidude: Fiery"
ckrack at i-z dot de
5.05.2004 18:39
Note that we can use $this in the parent's method like we might expect.
It will be used as the instance of the extended class.

<?php
class foo {
    var
$x;
    function
foofoo()
    {
       
$this->x = "foofoo";
        return;
    }
}
class
bar extends foo {
   
// we have var $x; from the parent already here.
   
function barbar()
    {
       
parent::foofoo();
        echo
$this->x;
    }
}
$b = new bar;
$b->barbar(); // prints: "foofoo"
?>
bitmore.co.kr
9.02.2004 22:13
/* Original */
/*
class A {
    var $x;
    function A() { $this->x = 32; }
    function joke() { print "A::joke" . A::x . "<br>\n"; # SYNTAX ERROR!! }
}
class B extends A {
    var $x;
    function B() {
        parent::A();
        $this->x = 44;
    }
    function joke() {
        parent::joke();
        print "B::joke " . $this->x . "<br>\n";
    }
}
echo A::joke();
echo B::joke();
*/

/* Original */
/* Modify */
/*
class A {
    var $x;
    function A() { $this->x = 32; }
    function joke() {
        $this = new A;
        print "A::joke = " . $this->x . "<br>\n";
    }
}
class B extends A {
    var $x;
    function B() {
        parent::A();
        $this->x = 44;
    }
    function joke() {
        parent::joke();
        $this = new B;
        print "B::joke = " . $this->x . "<br>\n";
    }
}

echo A::joke();
echo B::joke();

//print Show
A::joke = 32
A::joke = 32
B::joke = 44
*/
James Sleeman
10.01.2004 15:27
It should be noted that when using parent:: you are able to call a method defined within the parent, or any class that the parent is extending.. for example.

<?php
 
class A
 
{
    function
test()
    {
      echo
'Hello ';
    }
  }
 
  class
B extends A
 
{
   
  }
 
  class
C extends B
 
{
    function
test()
    {
     
parent::test();
      echo
'Bobby.';
    }
  }
 
 
$D =& new C();
 
$D->test();
?>

Outputs "Hello Bobby.", the fact that B does not define test() means that A's test() is called, it should also be noted that test() is called in the context of an 'A' class, so if you try and call parent:: in A's test() it will correctly fail.

It's also worth noting that parent:: also works fine in a static method, eg <?php  C::test(); ?> still outputs "Hello Bobby.", and you can even do <?php B::test(); ?> which correctly outputs "Hello ", even though we're calling a static method that lives in an ancestor class!
adrian dabrowski
15.09.2003 4:48
if you like to use the here suggested method of calling a parent constructer by using get_parent_class(), you will expirience undesired behaviour, if your parent is using the same technique.

class A {
  function A() {
     $me = get_class($this);
     echo "A:this is $me, I have no parent\n";
  }


class B extends A {
  function B() {
     $par = get_parent_class($this);
     $me = get_class($this);
     echo "B:this is $me, my parent is $par\n";
    
     parent::$par();
  }


class C extends B {
  function C() {
     $par = get_parent_class($this);
     $me = get_class($this);
     echo "C:this is $me, my parent is $par\n";
    
     parent::$par();
  }

new C();

this will not produce the output you probably expected:
C:this is c, my parent is b
B:this is b, my parent is a
A:this is a, i have no parent

...but insted you will get in serious troubles:
C:this is c, my parent is b
B:this is c, my parent is b

Fatal error:  Call to undefined function:  b() in .............. on line 16

$this is always pointing to the 'topmost' class - it seems like this is php's way to cope with polymorphic OOP.
hustbaer
24.08.2003 7:37
something on the note of 'minoc':

in this setup, the "$this->$par();" will lead to infinite recursion...

class foo extends bar {
  function foo() {

    $par = get_parent_class($this);
    $this->$par();

    // then normal constructor stuff.
    // ...
  }

  function bar()
  {
    echo "this makes no sense, but who cares :)";
  }
}

but the problem is easity fixed by going like so:

class foo extends bar {
  function foo() {

    $par = get_parent_class($this);
    parent::$par();

    // then normal constructor stuff.
    // ...
  }

  function bar()
  {
    echo "this makes no sense, but who cares :)";
  }
}
mazsolt at yahoo dot com
5.07.2003 12:04
There is a difference between PHP and C# managing the overloaded variables of the parent class:

<?php
class a{
var
$a=1;
function
display(){
echo
$this->a;
}
}

class
b extends a{
var
$a=9;
}

$x=new b;
$x->display(); // will display 9
?>

The same code in C# will display the value from the parent class.

[C#]
class a{
private int a=1;
public int display(){
return this.a;
}
}

class b:a{
private int a=9;
}

b x = new b();
Response.Write(b.display()); // will display 1
horne at teradyne dot com
26.03.2003 17:09
I've been scouring through the comments but haven't been able to find an example of how to use variable overriding. The manual hints that it is possible, but there aren't any examples. See the code for what I want to do:

class A {
  var $x;

  function A() {
    $this->x = 32;
  }

  function joke() {
    print "A::joke" . A::x . "<br>\n"; # SYNTAX ERROR!!
  }
}

class B extends A {
  var $x;

  function B() {
    parent::A();
    $this->x = 44;
  }

  function joke() {
    parent::joke();
    print "B::joke " . $this->x . "<br>\n";
  }
}

I want the following output when I call B::joke:

A::joke 32
B::joke 44

what do I get if I replace the A::x with $this->x?

A::joke 44
B::joke 44

How do I set the variable $x in the base class? Or, how do I get $this to point to the part of the class that is owned by class A?

I've tried the following (quite invalid) syntaxes:
A::x
A::$x
${A::x}
A::{$this->x}

It'd be nice to have a concrete example of how to access (read and write) parent variable members, or explicitly say that it is not possible to do that.
lelegiuly at iol dot it
24.03.2003 17:18
Sorry but my example above it's correct only if there's one class that inherits by another class. If the hierarchical tree is deeper than 2 my example doesn't work. The result is a infinite loop cycle.
Why? Because the only instanced object is the B class (in my example).

$this alwais refer to instanced object.

See also the "sal at stodge dot org" notes in 4 October 2002.
lelegiuly at iol dot it
6.03.2003 17:46
If you like Java and you have to call parent constructor, this is my method:

class object {
   function super() {
     $par = get_parent_class($this);
     $this->$par();
   }
}
class A extends object {
   function A() {
     echo "A constructor\n";
   }
}
class B extends A {
   function B() {
     $this->super();
     echo "B constructor\n";
   }
}

Include the classes above, instance a new B class and this is the output:

A constructor

B constructor

In your web application every class will extend the object class.
Good luck, Emanuele :)
Dan
2.03.2003 2:03
I agree with "anonymous coward". That syntax seems silly in a constructor and only results in an extra function being called. The only time the parent will ever change is if you change the extends expression in your class definition.

However, the parent:: syntax is very useful when you need to add extra functionality to a method of a child class which is already defined in the parent class.

Example:

class foo {
   var $prop;

   function foo($prop = 1) {$this->prop = $prop;}

   function SetProp($prop) {$this->prop = $prop;}
}

class bar extends foo {
   var $lastprop;

   function bar($prop = NULL) {
      is_null($prop) ? $this->foo() : $this->foo($prop);
   }

   function SetProp($prop) {
      $this->lastprop = $this->prop;
      parent::SetProp($prop);
   }
}
anonymous at cowards dot com
7.12.2002 21:48
Um, reading through all the gyrations on getting C++ constructor
semantics from PHP, I am wondering if I am just blindly missing something
when I use:

class A {
    function A () {  echo "A Constructor\n"; }
}

class B extends A {
    function B () { $this->A (); echo "B Constructor\n"; }
}

class C extends B {
    function C () { $this->B (); echo "C Constructor\n"; }
}

$c = new C ();

produces:

A Constructor
B Constructor
C Constructor

I'm new to PHP, but it seems to me that this is simpler and cleaner
than the solutions discussed here, with the only disadvantage being that
when you change the name of your superclass you have to change one
line in your subclass.

What am I missing? Are there advanced PHP-OO considerations that
make this code undesireable??
aid at logic dot org dot uk
5.12.2002 21:27
With regard to the chaining of Constrcutures, surely this is a simple and replaible method..?

<?php
class A
 
{
  function
A()
    {
    print
"Constructor A<br>\n" ;
    }
  }
 
class
B extends A
 
{
  function
B()
    {
   
parent::A () ;
    print
"Constructor B<br>\n" ;
    }
  }
 
class
C extends B
 
{
  function
C()
    {
   
parent::B ();
    print
"Constructor C<br>\n" ;
    }
  }
 
$o = new C;
?>
Which outputs,

Constructor A
Constructor B
Constructor C
sal at stodge dot org
4.10.2002 6:57
<p>When using the technique suggested by "<b>minoc at mindspring dot com</b>" on this page - USE EXTREME CAUTION.</p>

The problem is that if you inherit of a class that uses this kludge in the constructor you will end up with a recursive constructor - unless that new class has a constructor of it's own that by-passes the kludged constructor.

What I am saying, is you can use this technique as long as you can ensure that each class you inherit from a class containing this technique in the constructor has a constructor class of it's own that does not call the parent constructor...

On balance it is probably easier to specify the name of the constructor class explicitly!

If you make this mistake the symptoms are pretty easy to spot - PHP spirals into an infinitely recursive loop as soon as you attempt to construct a new class. Your web-server will returns an empty document.
mwwaygoo at hotmail dot com
20.09.2002 17:54
With the example given previous from johnremovethreewordsplatte@pobox.com

You should only do this if your initialisation is different (no point in extending if it isn't)

When you extend a class its constructor still remains valid and is executed when constructed.  Writing a new constructor in the extended class overwrites the old one - inheritance.

so the following will also work

class Superclass {
function Superclass($arg) {
$this->initialize($arg);
}
function initialize($arg) {
$this->setArg($arg);
}
}

class Subclass extends Superclass {
//initialize() is inherited:
//BUT so is constructor

function extra_stuff($arg) {
echo "extra stuff " . $arg;

}
}

THE constructor is the function who's name is the same as the class that it is defined within.
AND remains the constructor even in extended classes, until redefined.

Think of the constructor as a seperate function from all the others.  As if PHP looks at your class and makes a copy of the function of the same name as the class and calls it "Constructor".

You can also create a function within the extended class with the same name as the constructor in the parent class without re-writing the constructor.  Because you are not altering the "Constructor" function.
(BUT any redefined variables are used in the constructor, as expected)

ie
class One
{
  var $v = "one";

  function One()
  {
    print "Class One::Constructor value=" . $this->v . "<br />\n";
  }
}

class Two extends One
{
  var $v = "two";

  function one()
  {
    print "REDEFINED : Class Two::Function One value=" . $this->v . "<br />\n";
  }
}

$o = new One();
$o->one();
$p = new Two();
$p->one();

results in :-
Class One::Constructor value=one
Class One::Constructor value=one
Class One::Constructor value=two
REDEFINED : Class Two::Function One value=two
johnremovethreewordsplatte at pobox dot com
13.08.2002 2:16
Combining the above suggestions with the "Pull Up Constructor Body" refactoring from Fowler (http://www.refactoring.com/catalog/pullUpConstructorBody.html), I found an elegant way to inherit constructors in PHP. Both superclass and subclass constructors call an initialize() method, which can be inherited or overridden. For simple cases (which almost all of mine are), no calls to parent::anything are necessary using this technique. The resulting code is easy to read and understand:

class Superclass {
    function Superclass($arg) {
        $this->initialize($arg);
    }
    function initialize($arg) {
        $this->setArg($arg);
    }
}

class Subclass extends Superclass {
    function Subclass($arg) {
        //initialize() is inherited:
        $this->initialize($arg);
    }
}
gibby at onebox dot com
15.06.2002 13:25
Regarding the example above, I believe the result is misleading, if you're inferring that the $this in Class One's OneDo method refers to One.

If that wasn't your conclusion, then my apologies-- I misunderstood you.

But if it was, it might save trouble to note that $this is effectively an alias for $o (a Class Two object).  $v is available to $o, and that's how it's being picked up.  $this is $o in that example, not One (One is a class and has no implementation).

I hope that's helpful.

Brad
seedpod23 at hotmail dot com
29.05.2002 20:19
This example might illustrate something which was unclear to me when using the $parent::method() syntax.

I didn't see any documentation on whether or not $this (object variables) would be available in the parent class method so I wrote this example:

class One {

  var $v = "one";

  function OneDo() {
    print "One::OneDo " . $this->v . "<br>\n";
  }
}

class Two extends One {

  function CallParent() {
    parent::OneDo();
  }
}

$o = new Two();

$o->CallParent();

The output is "One::OneDo one", happy day!
php at earthside dot arg
9.04.2002 7:41
Here is the workaround I came up with based on the comments about using a 'constructor' method and the parent:: syntax...

#cat test.php
<?php
class foo {
  function
foo() { $this->constructor(); }
  function
constructor() {
    echo
get_class($this)." (foo)\n";
  }
}
class
bar extends foo {
  function
bar() { $this->constructor(); }
  function
constructor() {
    echo
get_class($this)." (bar)\n";
   
parent::constructor();
  }
}
class
baz extends bar {
  function &
baz() { $this->constructor(); }
  function &
constructor() {
    echo
get_class($this)." (baz)\n";
   
parent::constructor();
  }
}
$o = new baz;
?>
#php test.php
X-Powered-By: PHP/4.1.2
Content-type: text/html

baz (baz)
baz (bar)
baz (foo)
#

--
'a' -> 'o' in email TLD
minoc at mindspring dot com
8.09.2001 1:52
w/ PHP 4.0.5, I was able to chain
my constructors easily with:

class foo extends bar {
  function foo() {

    $par = get_parent_class($this);
    $this->$par();

    // then normal constructor stuff.
    // ...
  }
}
tim dot parkinson at ntlworld dot com
28.07.2001 14:37
There is an implication here that variables of the parent class are available.  You can't access variables using parent::
venome at gmx dot net
25.06.2001 17:57
If you have a complex class hierarchy, I find that it's a good idea to have a function constructor() in every class, and the 'real' php constructor only exists in the absolute base class.

From the basic constructor, you call $this->constructor(). Now descendants simply have to call parent::constructor() in their own constructor, which eliminates complicated parent calls.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)