【问题标题】:PHP PDO custom class returning an object and not what it shouldPHP PDO 自定义类返回一个对象而不是它应该返回的对象
【发布时间】:2012-06-06 19:15:34
【问题描述】:

所以我创建了一个数据库类来处理我所有的数据库请求。一切都通过构造函数,它应该返回值。

课程是这样的

<?php

class Database {

    /**
     * This array holds all of the configuration settings for the database
     * @var array
     */
    private $config = array(
        'username'  =>  '',
        'password'  =>  '',
        'host'      =>  '',
        'database'  =>  ''
    );

    /**
     * Holds the parameters passed to the class
     * @var mixed
     */
    private $parameters;

    /**
     * Database Handler
     * @var [type]
     */
    private $DBH;

    /**
     * Class constructor
     * @param [type] $action     [description]
     * @param [type] $parameters [description]
     */
    public function __construct($action, $parameters){
        $this->parameters = $parameters;

        $this->DBH = new PDO("mysql:host=".$this->config['host'].";dbname=".$this->config['database'], $this->config['username'], $this->config['password']); 

        return $this->$action();
    }


    private function query(){
        $STH = $this->DBH->prepare($this->parameters);
        $STH->execute();
        $result = $STH->fetchColumn();
        echo "<br><br>RESULT:".$result."<br><br><br>";
        echo "<br><br>RESULT:".empty($result)."<br><br><br>";

        return (empty($result)) ? FALSE : TRUE;
    }
} 

我删除了除功能问题之外的所有内容。它旨在返回真或假。相反,当我调用$result = new Database('query', $query); 时,返回值是一个包含大量数据的对象

知道我做错了什么吗?

【问题讨论】:

  • empty() 返回布尔值,所以 return !empty($result); 就足够了。
  • 我知道。我对代码感到沮丧,所以尝试了一切

标签: php mysql database pdo


【解决方案1】:

PHP 会忽略您在 __construct 中返回的内容。如果您使用new 创建一个新对象,则返回新对象,而不是__construct 中的return 所说的内容。

要实现您想要的,您必须创建一个新函数,该函数在构造函数之外为您执行操作 - 就像这样:

class Database {
    // your code...

    public function __construct($parameters) {
        $this->parameters = $parameters;

        $this->DBH = new PDO("mysql:host=".$this->config['host'].
            ";dbname=".$this->config['database'],
            $this->config['username'],
            $this->config['password']); 
    }

    public function perform($action) {
        return $this->$action();
    }

    // rest of your code...
}

// usage:
$db = new Database($query);
$result = $db->perform('query'); // result should be a boolean.

【讨论】:

  • 好的,但是我的return (empty($result)) ? FALSE : TRUE; 行不应该返回值吗?
  • @Sam: 是的,query 方法可能正在执行您想要的操作,但是由于您在构造函数中返回值,因此它会被静默丢弃。我现在已经编辑了我的答案并添加了一个可能的解决方案。
  • 我可以有一个像public $return; 这样的公共变量,然后在查询函数中创建对象后执行$this-&gt;return = $result$result = $this-&gt;return; 之类的操作
  • 您的意思是query 中的最后一行看起来像$this-&gt;result = (empty($result)) ? FALSE : TRUE; 而不是return (empty($result)) ? FALSE : TRUE?是的,这可能有效。你只需要像$result = (new Database('query', $query))-&gt;result 一样使用它——当然是使用你的__construct
【解决方案2】:

__construct 应该返回新创建的对象。无法覆盖此行为。见usage

顺便说一句,这是大多数 OOP 语言在涉及 new 运算符时的行为。

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2019-10-13
    • 2013-07-07
    • 2013-10-11
    • 2015-09-18
    相关资源
    最近更新 更多