views:

97

answers:

3

I would like to know if there is anyway I can make a parent object with php, I have tried this:

new parent::__construct($var);

but it doesn't work and I get the following error in the php logs:

(..)PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'(..)

+1  A: 

This might work:

$parentClass = get_parent_class();
$parentObject = new $parentClass();
nickf
+4  A: 

see http://uk.php.net/get_parent_class

<?php
class Foo {

}

class Bar extends Foo {
  protected $p;

  public function __construct() {
    $pc = get_parent_class();
    $this->p = new $pc;
  }
}

$bar = new Bar;
var_dump($bar);

(But somehow I fail to see why you would need something like that. But maybe that's just me.... ;-))

VolkerK
+1 - i didn't know about that function.
nickf
Me neither But hey, it's php. "I want to get the parent class" -> http://php.net/get_parent_class taadaa... ;-D
VolkerK
+1  A: 

Just call the constructor of the parent class like:

$parentClassObject = new ParentClassName();

Use parent::__construct() to call the parent class constructor since it is not done automatically in PHP.

andreas