方法MyClass :: __ get()必须正好使用1个参数

时间:2013-01-19 16:17:03

标签: object php

我在尝试创建班级的新实例时遇到错误。

class MyClass
{    
    public $link;

    public function __construct()
    {
        $this->Connect();
    }

    private function Connect()
    {
        $db_host = DB_HOST; // defined in included file
        $db_name = DB_NAME; // defined in included file

        $this->link = new PDO("mysql:host=$db_host;dbname=$db_name;charset=UTF-8", DB_USER, DB_PASSWORD);

        $this->link->setAttribute(PDO::ATTR_AUTOCOMMIT,FALSE);

    }

    // other methods here
    // there is no custom __get() method
}

我正在尝试创建新实例并使用其中一种方法:

include "inc/myclass.php";
$db = new MyClass();
$db->InsertPost("2012-01-01 10:00", "Test content", "Test title");

我得到错误:

  

方法MyClass :: __ get()必须正好使用1个参数

我尝试添加没有任何参数的访问者:

public function __get() 
{
    return $this;
}

但我仍然遇到这个错误。

1 个答案:

答案 0 :(得分:0)

我必须覆盖默认访问者(get),如下所示:

public function __get($name) 
{
    return $this;
}

我不明白为什么我需要$name,但它完美无缺。