9

Pretty short question, here is an example:

$prepared = $this->pdo->prepare("SELECT * FROM Users WHERE ID = :ID");
$statement = $prepared->execute(array(":ID" => $User_ID))
$result = $statement->fetchAll(PDO::FETCH_CLASS, "User");
//OR
$User = new User();
$result = $statement->fetch(PDO::FETCH_INTO, $User);

(written from top of the head, could contain syntax errors)

Do those two directly fetch into the private properties of said objects? I read it also circumvents the __construct function, so will it circumvent private status too?

4 Answers 4

7

Very short answer: Yes it will.

class Foo
{
    private $id;
    public function echoID()
    {
        echo $this->id;
    }
}
$result = $statement->fetchAll(PDO::FETCH_CLASS, "Foo");
$result[0]->echoID(); // your ID

Aside:

This will cause syntax errors $statement->fetchAll(PDO::FETCH_INTO, $User);. You can't use FETCH_INTO with the fetchAll method.

Sign up to request clarification or add additional context in comments.

6 Comments

Ah, okay... Since fetchAll wants to create more than one object this will never work. I get it!
It will have no problem creating the classes, it is FETCH_INTO that won't work since you can only send a single object as a parameter.
@Wesleay that's what I was refering to. I only give it a single instance so it doesn't work.
PDO::FETCH_INTO will give you exception cannot access protected property. However, PDO::FETCH_CLASS works fine.
-1 That is incorrect. FETCH_INTO cannot fetch into private properties. Please test your answers.
|
2

But event with PDO::FETCH_CLASS there is a problem for private properties for subclasses. E.g.

class Animal
{
    private $color;
    public function getColor()
    {
        return $this->color;
    }
}
class Cat extends Animal
{
}

$statement->setFetchMode(PDO::FETCH_CLASS, "Cat" );
$someCat = $statement->fetch();

echo $someCat->getColor();  //empty
print_r( $someCat );
/*
now have strange output like:
[color:Animal:private] => 
[color] => grey
*/

But if you set the property to protected - it works fine

1 Comment

Private properties belong to the class only and cannot be inherited by subclasses.
0

The reason you can't access private properties on a super class is because those properties are out of scope. Subclasses don't take on the private attributes of their parent classes, including variables and functions.

edit: Thanks for clarifying your question but it does make my answer look a bit ridiculous here. :p

Comments

0

You could try:

class Foo {
    private $id;
    public function __set($prop, $val) {
        $this->$prop = $val;
    }
    public function __get($prop) {
        return $this->$prop;
    }
}

$result = $statement->fetchAll(PDO::FETCH_CLASS, "Foo");
$result[0]->id();

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.