【问题标题】:Get StdClass Object Property Name获取 StdClass 对象属性名称
【发布时间】:2013-08-12 14:11:17
【问题描述】:

我将以下对象传递给函数:

$this->cars->car1->engine='v8';
$this->cars->car1->hp='330';

$this->cars->car2->engine='v6'
$this->cars->car1->hp='210';

handle_car($this->cars->car1);
handle_car(this->cars->car2);

函数handle_car需要找出它是什么车,即它是“car1”还是“car2”。

如何通过属性键查看属性是 car1 还是 car2?我曾尝试使用 get_object_vars 函数,但它只返回“car1”和“car2”的键,而不是它们本身。

【问题讨论】:

    标签: php object stdclass


    【解决方案1】:

    您将属性值传递给您的函数,而不是属性名称。您应该直接传递名称以在函数内部正确确定它。请记住,$this->cars->car1$this->cars->car2 只是对象,可能是任何东西。

    【讨论】:

    • 所以你说不可能?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多