【问题标题】:Getting properties of an object retrieved from DB by PDO fetchAll (PDO::FETCH_OBJ)通过 PDO fetchAll (PDO::FETCH_OBJ) 获取从 DB 检索到的对象的属性
【发布时间】:2019-01-06 16:52:08
【问题描述】:

我确实有一个使用 PDO fetchALL(PDO::FETCH_OBJ) 从 DB 中检索到的对象数组。当我 var_dump 该数组的第一个元素时: var_dump($this->stockList[0]); 我明白了:

object(stdClass)[5]
public 'userId' => string '3' (length=1)
public 'symbol' => string 'ibm' (length=3)
public 'date' => string '2019-01-03' (length=10)
public 'quantity' => string '5' (length=1)
public 'bought' => string '1' (length=1)
public 'observed' => string '0' (length=1)
public 'dividendRate' => string '6.28' (length=4)
public 'exDividendDate' => string '2018-11-08' (length=10)
public 'forwardDividend' => string '31.400000000000002' (length=18)

我想在这个对象上使用反射来获取它的所有属性:

$r = new ReflectionClass($this->stockList[0]);
$objProperties = $r->getProperties();

我得到了正确的课程: var_dump($r); 产生:object(ReflectionClass)[16] public 'name' => string 'stdClass' (length=8)

但我无法获取该对象的属性: var_dump($objProperties); 给出一个空数组: array (size=0) empty 那么,问题是如何获取该对象的属性列表? 我的完整代码:

$sql = "query";
$this->stockList = $this->con->query($sql)->fetchAll(PDO::FETCH_OBJ);

var_dump($this->stockList[0]);
$r = new ReflectionClass($this->stockList[0]);
$objProperties = $r->getProperties();
var_dump($r);
var_dump($objProperties);

【问题讨论】:

  • 为什么需要反射才能做到这一点? foreach($stockList as $stockItem) echo $stockItem->userId; - 所有参数已经公开。如果要获取参数名称,请考虑使用PDO::FETCH_ASSOC,然后使用array_keys(),它将保存['id', 'symbol', etc..]
  • 或者在依赖PDO::FETCH_OBJ时只是array_keys(get_object_vars($this->stockList[0]))
  • @Quasimodo'sclone 甚至都不想使用get_object_vars()。很好的建议。
  • @Jaquarh,您可以在更广泛的答案中引用此内容。
  • get_object_vars() 太棒了!谢谢。出于好奇 - 为什么反射不起作用?我在自己的课程中多次使用它,效果很好。 @Jaquarh - 我使用使用 FETCH_OBJ 作为标准的代码,所以不想改变它。

标签: php pdo


【解决方案1】:

使用 StdClass 的反射将不起作用。对ReflectionClass() 的调用,参数一使用实例的::class 来确定其属性。由于 StdClass 默认没有属性并且是动态给出的,因此反射找不到任何属性,因为默认情况下它们不存在。

您可以在demo 中看到上述内容。但是,为了更简单,这可以正常工作:

var_dump(array_keys((array) new Foo('bar'))); # Based on above demo

但是,不要惊慌。您不需要使用反射来执行此操作:\PDO::FETCH_ASSOC 将为您提供一个多维数组。您可以使用array_keys() 获取参数。然后,稍后,如果您更喜欢将结果用作对象,请将数组转换为对象。

# Fetch Query
$this->stockList = $this->con->query($sql)->fetchAll(PDO::FETCH_ASSOC);

# Get properties which will be, in this case, ['id', 'symbol', 'date', 'quantity', 'bought', 'observed', 'dividendRate', 'exDividentDate', 'forwardDivident']
$properties      = array_keys($this->stockList[0]);

# Revert all back to StdClass instances by casting the array to object
foreach($this->stockList as &$stockItem) {
    $stockItem = (object) $stockItem;
}

另外,正如@Quasimodosclone 在 cmets 中所建议的那样。您可以使用get_object_vars(),它将返回对象的等效数组。然后,像以前一样,使用array_keys() 获取属性。

$this->stockList = $this->con->query($sql)->fetchAll(PDO::FETCH_OBJ);
$properties      = array_keys(get_object_vars($this->stockList[0]));

出于好奇对此进行了测试后,可以将对象强制转换为数组以实现更简单。

$this->stockList = $this->con->query($sql)->fetchAll(PDO::FETCH_OBJ);
$properties      = array_keys( (array) $this->stockList[0] );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2016-01-19
    • 2021-12-09
    • 1970-01-01
    • 2014-11-23
    • 2015-06-13
    相关资源
    最近更新 更多