0

私はPDOを使用するのがとても好きなので、オンになっている可能性があります。

使用法は次のようになります。

$m = new MDB();

$m->Users()->GetRec($param);

Users()データベース内のテーブルの名前であり、GetRec($param)私の関数です。

私がこのように見えるようにする方法:

class MDB extends DB {

    function __construct(){

        parent::__construct();

        if ($result = $this->pdo->query("SHOW TABLES"))
        {

            while ($row = $result->fetch(PDO::FETCH_NUM))
            {
                // this is only my imagination (not working at all)
                __set($row[0],true);

            }

        }

    }

    // set
    public function __set($name, $value)
    {
        // here could be a method (not properties)
        $this->$name = $value;
    }

確かに、それはすべて私が望んでいるものではないようです。だから私はこの質問でいくつかの提案やアドバイスを得ることができます。

upd1

魔法のメソッド__callをありがとう、そして今私はそれを内に作ろうとしています。私の更新されたコードを見てください:

class MDB extends DB {

    function __construct(){

    parent::__construct();

}

public function __call( $method, $param )
{

    $tables = array();

    if ($result = $this->pdo->query("SHOW TABLES"))
    {

        while ($row = $result->fetch(PDO::FETCH_NUM))
        {

            $tables[] = $row[0];

        }

    }

    if (in_array($method,$tables))
    {

        return $this;

    }
    else
    {

        return FALSE;

    }

}

まあ、それは私のために働くようです!

4

1 に答える 1

0

はい、可能です!コードの間違った行は次のとおりです。

__set($row[0],true);

そこであなたは呼ぶべきです:

$this->$row[0] = true;

以下の簡単な例と、PHPhttp://www.php.net/manual/en/language.oop5.overloading.phpのオーバーロードドキュメントをご覧ください

class A{

//The array which will contain your data
private $tables;

function __construct($array){
    $counter = 1;
    foreach($array as $value){
        //That's the right call!    
        $this->$value = $counter++;
    }
}


function __set($name, $value){
    $this->tables[$name] = $value;
}

function __get($name){
    if (array_key_exists($name, $this->tables)) {
           return $this->tables[$name];
    }
}

}

$array = array('a1', 'a2', 'a3', 'a4', 'a5');
$a = new A($array);

foreach($array as $key){
    echo $a->$key;
}

この助けを願っています!

于 2012-08-18T06:43:24.400 に答える