2010-07-23 115 views
0

养活一类参数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) 
+0

该代码给了我没有错误。 – 2010-07-23 20:07:02

+0

适合我。错误消息报告的行号是什么,你的'Bar'构造函数是否真的与该行对应? – BoltClock 2010-07-23 20:07:46

+0

项目的主要代码很长!但我相信那种模式就是这样!何时以及为何发生这种错误? – Jalal 2010-07-23 20:14:43

回答

2

谢谢你的完整的代码,这里是你的错误:

public function __construct(data\DBConnection $connection, int $type = 0) 

PHP 5.3只支持类型提示的阵列和类。

你的int $type = 0声明寻找类命名为“INT”,并分析它是这样,即使该类不存在。

您需要删除虚假类型提示。 PHP开发人员是considering adding other type hints in the future。有currently code in PHP's trunk为标量值执行类型提示,但自从发布该博客文章以来,发生了很多变化。我会看看我是否可以追踪目前的状况,但是搜索PHP内部邮件列表是一件非常可怕的任务。


编辑:通过PHP内部挖掘后,我发现the latest Type Hinting线程。它看起来像this the current Type Hinting RFC,但在方向上似乎没有达成共识。 Zeev想要回滚提交,但尚不清楚这是否完成。

+0

+1即将发布“发布你的答案”,并出现可怕的橙色吧... – BoltClock 2010-07-23 20:25:55

+0

敦敦敦.... – Charles 2010-07-23 20:27:46

+0

谢谢你的所有答复! – Jalal 2010-07-23 20:35:49