如何通过构造传递带参数的对象

时间:2012-02-11 15:43:45

标签: php class constructor

我有一个类依赖于其他几个类。因此,我通过我的构造函数传递这些其他类,这一切都正常。但是,当我传递一个需要为自己的构造函数提供参数的类时,我收到一条错误,上面写着'缺少参数1 for mysql :: __ construct()...'。在具有必需参数的类中传递对象的最佳方法是什么?

如果您发现此设计存在任何潜在缺陷,请告知我们。我对面向对象相当新,结果还在学习。

我目前拥有和不工作的一个例子:

class mysql{
    function __construct($memcache){
        // Construct the object 
    }
}
class company{
    function __construct($databases,$connections,$mysql,$facebook){
        // Construct the object

        // Make the MySQL class available to all the methods
        $this->mysql = $mysql;
    }
}

// Initialize the objects (Company, MySQL, Memcache and Facebook)
$memcache = new memcache;
$facebook = new Facebook($facebook_config);
$mysql = new mysql($memcache);
$company = new company($_DATABASES,$_CONNECTIONS,$mysql,$facebook);

已解决UDPATE

上面的代码实际上是正确的,问题是因为我在我的一个方法结束时调用__construct()而我忘记传递$ memcache参数

0 个答案:

没有答案