开发者

Mysterious missing class values with pg_fetch_object()

开发者 https://www.devze.com 2023-03-10 00:13 出处:网络
i am having a problem where pg_fetch_object() returns an empty object when I pass a custom class. class foo {

i am having a problem where pg_fetch_object() returns an empty object when I pass a custom class.

class foo {
    protected $_data = array();

    public function __construct(array $data = array()) {
        $this->_data = array(
            'foo_id' => null,
            'parent_id'  => null,
            'type' => 'region',
            'name' => null,
            'description' => '',
        );

        foreach ($data as $key => $val) {
            $this->__set($key,$val);
        }
    }

    public function __set($key, $val) {
        echo "SETTING $key -> $val";
        var_dump($this->_data);
        $this->_data[$key] = $val;
    }

    public function __get($key) {
        return $this->_data[$key];
    }开发者_运维知识库
}

i then call this

 $tmp = pg_fetch_object($result, $current_row, "foo");
 var_dump($tmp);

and i get this output

SET foo_id => 55925
array(0) { }
SET name => All
array(1) { ["foo_id"]=> string(5) "55925" }
SET searchable => ALL
array(2) { ["foo_id"]=> string(5) "55925" ["name"]=> string(3) "All" }
SET parent_id =>
array(3) { ["foo_id"]=> string(5) "55925" ["name"]=> string(3) "All"                 ["searchable"]=> string(3) "ALL" }
SET type => region
array(4) { ["foo_id"]=> string(5) "55925" ["name"]=> string(3) "All"     ["searchable"]=> string(3) "ALL" ["parent_id"]=> NULL }
SET sub_count => 96
array(5) { ["foo_id"]=> string(5) "55925" ["name"]=> string(3) "All" ["searchable"]=> string(3) "ALL" ["parent_id"]=> NULL ["type"]=> string(6) "region" }
SET description =>
array(6) { ["foo_id"]=> string(5) "55925" ["name"]=> string(3) "All" ["searchable"]=> string(3) "ALL" ["parent_id"]=> NULL ["type"]=> string(6) "region"     ["sub_count"]=> string(2) "96" } asdfasdfasdfasdfasdf

object(Dao_Region)#23 (2) {
  ["_data":protected]=>
  array(5) {
    ["foo_id"]=>
    NULL
    ["parent_id"]=>
    NULL
    ["type"]=>
    string(6) "region"
    ["name"]=>
    NULL
    ["description"]=>
    string(0) ""
  }
}

why does my $tmp variable have all initial values, even though my setters fired ?

strangly when i change "foo" to StdClass in the pg_fetch_object() call, it works.


pg_fetch_row instantiates the class you pass to it and sets properties named after the field names in the database.

EG, pg_fetch_row does something like:

$record = pg_fetch_assoc()
$class = new $passedClassName();
foreach($record as $field => $val) $class->$field = $val;
return $class;

It does not pass an array of its values to a constructor, as you have coded the example above.

So, pg_fetch_row will instantiate the foo class as normal, passing no parameters to its constructor, your code allows this and defaults the $data param to an empty array, sets the default values, and exits.

The calls to __set that we see in the log are triggered in the foreach loop in my faked up example. Something odd IS happening there; the values ought to be in the array. Try removing the constructor and seeing if that fixes things...

0

精彩评论

暂无评论...
验证码 换一张
取 消