dongtao9095 2013-07-10 09:08
浏览 40
已采纳

OOP PHP - 通过函数传递方法[重复]

This question already has an answer here:

I am new to OOP in PHP and I am trying to understand how it work. I am trying to implement PDO into a my own class.

This is what I am doing:

class db {

    private $options;

    function __construct() {
        $this->options = array(
            'database_host' => DATABASE_HOST,
            'database_name' => DATABASE_NAME,
            'database_user' => DATABASE_USER,
            'database_pass' => DATABASE_PASS
        );

    }

    private function connect() {
        try {
            $pdo = new PDO('mysql:host=' . $this->options['database_host'] . ';dbname=' . $this->options['database_name'], $this->options['database_user'], $this->options['database_pass']);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $pdo->exec("set names utf8");
        } catch (PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
    }

    public function select($table, $what, $where, $custom) {

        $query = "SELECT " . $what . " FROM " . $table;
        $child = "1";
        $param = array();
        if ($where)
            foreach ($where as $data => $value) {
                if ($child == "1") {
                    $query .= " WHERE " . $data . " = '" . $value . "'";
                    $param[":" . $data] = $value;
                    $child = "next";
                } else {
                    $query .= " AND " . $data . " = '" . $value . "'";
                    $param[":" . $data] = $value;
                }
            }

        if ($custom)
            $query .= ' ' . $custom;

        $statement = $pdo->prepare($query);
        if ($statement->execute($param))
            return $statement->fetchAll(PDO::FETCH_ASSOC);
        return false;
    }

}

The problem is that I don't know how to make the function connect() communicating with the below one select() When I try to call the select() function, I get this error:

Fatal error: Call to a member function prepare() on a non-object in /Applications/MAMP/htdocs/game/api/class.db.php on line 47

I would like that when I call the select() function, the connect() one connects to the DB and makes all its variables available for the other functions of the class...but I am lost.

</div>
  • 写回答

2条回答 默认 最新

  • doulun1915 2013-07-10 09:11
    关注

    Store the $pdo variable as a property. Currently it is just a variable local to the connect() method.

    class db {
        private $options;
        private $pdo;
    }
    

    Then use $this->pdo to access it from any of the methods. For example in your connect():

    private function connect() {
        try {
            $this->pdo = new PDO('mysql:host=' . $this->options['database_host'] . ';dbname=' . $this->options['database_name'], $this->options['database_user'], $this->options['database_pass']);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->exec("set names utf8");
        } catch (PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
    }
    

    Do the same for your other methods, e.g.:

    $statement = $this->pdo->prepare($query);
    

    You might prefer to extend the PDO class, rather than having a new class that keeps a property of the PDO object. The difference is when extending it, your db class will behave the same as the PDO class, except it will have your additional methods available. Whereas with your current code, the db class only has methods that you explicitly define.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?