SandroMarques
SandroMarques

Reputation: 6534

Return type for PHP method which returns a database object?

What return type I should replace in "WHAT?" text. And if the query returns no results?

I'm using PHP 7.1

The method is:

public function retrieveSomething(int $id_user): WHAT? {
    try {
        $this->sql = 'SELECT * FROM tbl_users WHERE IdUser = :id_user';
        $stmt = $this->db->prepare($this->sql);
        $stmt->bindValue(':id_user', $id_user, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_OBJ);
        $stmt->closeCursor();
        return $row;
    } catch (PDOException $e) {
        throw $e;
    } catch (Exception $e) {
        throw $e;
    }
}

Upvotes: 2

Views: 2668

Answers (3)

SandroMarques
SandroMarques

Reputation: 6534

This is a post for my own question because I want to show the final code.

public function retrieveSomething(int $id_user): ?\stdClass {
    $this->sql = 'SELECT * FROM tbl_users WHERE IdUser = :id_user';
    $stmt = $this->db->prepare($this->sql);
    $stmt->bindValue(':id_user', $id_user, PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_OBJ);
    $stmt->closeCursor();
    if ($row) {
        return $row;
    }
    return null;
}

Both Álvaro González and Dymen1 are correct...the return type is stdClass, but I needed to use the backslash \stdClass. Maybe because I'm using Composer autoload.

If the query returns no results, the return value will be boolean(false) and the method throw the TypeError. To correct this I put the question mark ?\stdClass. The PHP documentation says

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark

PHP 7 New features

Upvotes: 2

Álvaro González
Álvaro González

Reputation: 146390

From manual:

PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set

You have some examples among the user comments:

object(stdClass)#6 (3) {
  ["1"]=>
  string(1) "1"
  ["2"]=>
  string(1) "2"
  ["3"]=>
  string(1) "3"
}

So:

public function retrieveSomething(int $id_user): \stdClass {
}

... should work (I don't have PHP/7 here to test).

Also:

The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.

To avoid this, you should configure PDO to throw exceptions (which you apparently already did, and is a good idea by itself).

Additionally, I suggest you drop the try/catch blocks, they're completely redundant.

Upvotes: 4

Dymen1
Dymen1

Reputation: 736

From the docs:

The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure. PDO::FETCH_OBJ: returns an anonymous object

This would result in:

function retrieveSomething(int $id_user): stdClass {

But, what if the fetch fails? So you might want to check the result before returning it, that way you can prevent displaying errors to your users.

Upvotes: 1

Related Questions