养活一类参数i声明的类是这样的:在主代码错误,而试图在PHP
class Foo{ public function __construct(){ echo 'Foo was created!';} }
class Foo2 extends Foo{ public function __construct(){ parent::__construct(); echo 'Foo2 was created!';} }
class Bar{
public function __construct(Foo $foo){ echo 'Bar was created!';}
}
:
$foo2 = new Foo2();
$bar = new Bar($foo2);
这是什么错误主代码的原因:
Fatal error: Default value for parameters with a class type hint can only be NULL
php版本:PHP 5.3.2
--------------------------------------更新!--------- -------------------------------
文件:system.data.php
namespace system\data{
include ('system.php');
use system;
class DBConnection implements system\IDisposable {
protected $serverName;
protected $userId;
protected $password;
protected $handler;
protected $isOpened;
/*
* create a new instance of DBConnection.
*/
public function __construct($server, $uid, $password) {
$this->isOpened = false;
$this->serverName = $server;
$this->userId = $uid;
$this->password = $password;
}
class DBCommand implements \system\IDisposable {
public function __construct(DBConnection $connection, int $type) {
$this->connection = $connection;
$this->queryType= $type;
}
}
文件:system.data.mysql.php
namespace system\data\mysql{
class MySqlCommand extends DBCommand {
public function __construct(data\DBConnection $connection, int $type = 0) {
parent::__construct($connection, $type);
}
}
class MySqlConnection extends DBConnection {
public function __construct($server, $uid, $password) {
parent::__construct($server, $uid, $password);
}
}
}
错误:
Fatal error: Default value for parameters with a class type hint can only be NULL in C:\Program Files\Apache Software Foundation\...\system.data.mysql.php on line 35(constructor declartion)
该代码给了我没有错误。 – 2010-07-23 20:07:02
适合我。错误消息报告的行号是什么,你的'Bar'构造函数是否真的与该行对应? – BoltClock 2010-07-23 20:07:46
项目的主要代码很长!但我相信那种模式就是这样!何时以及为何发生这种错误? – Jalal 2010-07-23 20:14:43