通过对象调用公共变量

时间:2013-06-14 21:21:56

标签: php function object constructor pdo

$sql=$pardConfig->prepare("SELECT * FROM ".$this->menu);我想动态调用db表。在这里它只选择了menu.i想从对象中调用它?

 <?php

    $dbhost=null;
    $dbname=null;
    $dbuser=null;
    $dbpass=null;

    $file = __DIR__ ."/config.json";

    $array = file_get_contents($file);
    $dbConfig=json_decode($array);

    $pardConfig=new PDO('mysql:host='.$dbConfig[0].';'.'dbname='.$dbConfig[1],$dbConfig[2],$dbConfig[3]);

    class pardDb
    {
        public $config = "pard_admin_config";
        public $article = "pard_article";
        public $menu = "pard_menu";
        public $user = "pard_user";
        public $images = "pard_images";

        function pardTemplate($pardConfig,$pardDbTable){
            $sql=$pardConfig->prepare("SELECT * FROM ".$this->menu);
            $sql->execute();
            $result=$sql->fetchALL(PDO::FETCH_OBJ); 
            $item = array_reverse($result);
            return $item;
        }
    }

    $pardDbTable = new pardDb();
    $pardDbTable->pardTemplate($config,$pardConfig);

    ?>

我想要一个对象,需要像这样调用它吗?

    echo $obj->menu; 
echo $obj->article; 

1 个答案:

答案 0 :(得分:0)

在一个设计模式中尝试重构这个(通过Active Record和Data Mapper开始你的学习)添加继承:

http://martinfowler.com/eaaCatalog/

看到Magic Methods __get和__set

之后

http://php.net/manual/en/language.oop5.overloading.php#object.get