简体   繁体   English

如何在PHP中获取对象的属性

[英]How to get the Property of an Object in PHP

I have an object that looks like this and has a This is a demo Variable property I want to access. 我有一个看起来像这样的对象,并且有一个“ This is a demo Variable我想访问This is a demo Variable属性”。

PHP对象的屏幕截图


I have the object saved in a variable called kpi : 我将对象保存在名为kpi的变量中:

$kpi = new Kpi($item);

And now I want to access the variable, but doing the following returns null : 现在,我想访问该变量,但是执行以下操作将返回null

dd($kpi->demoVariable) // null

How do I access the properties I set in my constructor? 如何访问在构造函数中设置的属性?

For reference, here is the constructor function: 供参考,这是构造函数:

public function __construct(Fuelconsumption $item)
{
    $this->demoVariable = 'This is a demo Variable';
}

Edit: 编辑:

I guess I would need to assign the properties to the attributes array, since it seems like laravel gets access to this array, by the above stated commands. 我想我需要将属性分配给attributes数组,因为看起来laravel通过上述命令可以访问此数组。

Just: 只是:

$kpi['demoVariable'];

Instead of 代替

$kpi->demoVariable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM