0

クラスの新しいインスタンスを作成しようとすると、エラーが発生します。

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;
}

しかし、私はまだこのエラーを取得しています。

4

1 に答える 1

0

次のように、デフォルトのアクセサー (get) をオーバーライドする必要がありました。

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

なぜそこに必要なのかわかりません$nameが、完璧に機能します。

于 2013-01-20T01:27:09.100 に答える