1

So I made a database class to handle all of my database requests. Everything goes through the constructor and it should return values.

The class is so

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

I removed everything bar the function giving issues. It is meant to return true or false. Instead the return value when I call $result = new Database('query', $query); is an object with a ton of data

Any idea what I have done wrong?

2
  • empty() returns boolean, so return !empty($result); would suffice. Commented Jun 6, 2012 at 19:21
  • I know. I was getting frustrated with the code so was trying everything Commented Jun 6, 2012 at 19:25

2 Answers 2

2

PHP ignores what you return in __construct. If you create a new object with new then the new object is returned and not what the return in __construct says.

To achieve what you want you have to create a new function which executes the action for you outside of the constructor - like that:

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Ok but what about my return (empty($result)) ? FALSE : TRUE; line Should that not return a value?
@Sam: yes the query method is probably doing what you want, but since you return the value in the constructor it is silently discarded. I have now edited my answer and added a possible solution.
Could I have a public variable like public $return; and then after creating the object in the query function do something like $this->return = $result and then $result = $this->return;
You mean the last line in query would look like $this->result = (empty($result)) ? FALSE : TRUE; instead of return (empty($result)) ? FALSE : TRUE ? Yes that might work. You just would have to use it like $result = (new Database('query', $query))->result - using your __construct of course.
1

__construct is suposed to return the newly created object. This behaviour cannot be overriden. See usage.

Btw, this is the behaviour for most OOP languages when the new operator is involved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.