【问题标题】:call to public variables to via object通过对象调用公共变量
【发布时间】:2013-06-14 21:21:55
【问题描述】:

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

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

【问题讨论】:

    标签: php function object constructor pdo


    【解决方案1】:

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

    http://martinfowler.com/eaaCatalog/

    在看到魔术方法 __get 和 __set 之后

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

    【讨论】:

      猜你喜欢
      • 2014-04-05
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 2018-01-20
      • 1970-01-01
      • 2019-09-18
      • 2019-12-20
      相关资源
      最近更新 更多