我想知道无论如何我可以用php创建一个父对象,我试过这个:
new parent::__construct($var);
但它不起作用,我在 php 日志中收到以下错误:
(..)PHP 解析错误:语法错误,意外的 T_STRING,期待 T_VARIABLE 或 '$'(..)
我想知道无论如何我可以用php创建一个父对象,我试过这个:
new parent::__construct($var);
但它不起作用,我在 php 日志中收到以下错误:
(..)PHP 解析错误:语法错误,意外的 T_STRING,期待 T_VARIABLE 或 '$'(..)
见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);
(但不知何故,我不明白你为什么需要这样的东西。但也许这只是我.... ;-))
这可能有效:
$parentClass = get_parent_class();
$parentObject = new $parentClass();
只需调用父类的构造函数,如:
$parentClassObject = new ParentClassName();
使用 parent::__construct() 调用父类构造函数,因为它在 PHP 中不会自动完成。