Using PDO::FETCH_CLASS with Magic Methods

前端 未结 1 1419
-上瘾入骨i
-上瘾入骨i 2020-12-16 21:23

I have a class that uses magic methods to store properties. Here is a simplified example:

class Foo {
    protected $props;

    public function __construct         


        
相关标签:
1条回答
  • 2020-12-16 21:36

    The default behavior of PDO is to set the properties before invoking the constructor. Include PDO::FETCH_PROPS_LATE in the bitmask when you set the fetch mode to set the properties after invoking the constructor, which will cause the __set magic method to be called on undefined properties.

    $st->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Foo');
    

    Alternatively, create an instance and fetch into it (i.e. set fetch mode to PDO::FETCH_INTO).

    0 讨论(0)
提交回复
热议问题