简体   繁体   English

无法从 object PHP 获取属性值

[英]can't get property value from object PHP

I am facing a weird issue with the following object,我面临以下 object 的奇怪问题,

$ban = (object) array('type'=> 'mute', 'reason', 'asdasd', 'curr_ts', 1614661389, 'timestamp', 1614751450, 'bannedby', 'shoyeb');
var_dump($ban->type); // shows 'mute'
var_dump($ban->reason); // shows null
var_dump($ban->curr_ts); // shows null
var_dump($ban->timestamp); // shows null
var_dump($ban->bannedby); // shows null

I can only get the value of property 'type' and can't access other properties.我只能获取属性“类型”的值,不能访问其他属性。

Please Refer Man Page of PHP Array for better understanding :请参阅 PHP 阵列的手册页以获得更好的理解

values Syntax "index => values" , separated by commas, define index and values. values 语法"index => values" ,用逗号分隔,定义索引和值。 index may be of type string or integer.索引可以是字符串或 integer 类型。 When index is omitted, an integer index is automatically generated, starting at 0 .省略索引时,会自动生成一个 integer 索引,从 0 开始 If index is an integer, next generated index will be the biggest integer index如果索引是 integer,下一个生成的索引将是最大的 integer 索引

    1. Note that when two identical index are defined, the last overwrite the first.请注意,当定义了两个相同的索引时,最后一个会覆盖第一个。

Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.在最后一个定义的数组条目之后有一个尾随逗号虽然不常见,但它是一种有效的语法。

You have:你有:

$ban = (object) array('type'=> 'mute', 'reason', 'asdasd', 'curr_ts', 1614661389, 'timestamp', 1614751450, 'bannedby', 'shoyeb');

And you're getting below which is correct而你正在低于哪个是正确的

var_dump($ban->type); // shows 'mute'
var_dump($ban->reason); // shows null
var_dump($ban->curr_ts); // shows null
var_dump($ban->timestamp); // shows null
var_dump($ban->bannedby); // shows null

Because your object is as follows:因为你的 object 如下:

stdClass Object
(
    [type] => mute
    [0] => reason
    [1] => asdasd
    [2] => curr_ts
    [3] => 1614661389
    [4] => timestamp
    [5] => 1614751450
    [6] => bannedby
    [7] => shoyeb
)

To understand better follow below:为了更好地理解以下内容:

[akshay@db1 tmp]$ cat test.php 
<?php

$ban = (object) array('type'=> 'mute', 'reason', 'asdasd', 'curr_ts', 1614661389, 'timestamp', 1614751450, 'bannedby', 'shoyeb');

# object contents
print_r($ban);

# for property 0
echo $ban->{'0'}.PHP_EOL;
 
[akshay@db1 tmp]$ php test.php 
stdClass Object
(
    [type] => mute
    [0] => reason
    [1] => asdasd
    [2] => curr_ts
    [3] => 1614661389
    [4] => timestamp
    [5] => 1614751450
    [6] => bannedby
    [7] => shoyeb
)

reason

If you wish to access like $ban->reason then you need to define like below如果你$ban->reason一样访问,那么你需要定义如下

$ban = (object) array('reason'=> 'somevalue or null' );

OR standard way或标准方式

$ban = new stdClass();
$ban->reason = 'foo';
print_r($ban);

You can change the array into something like this:您可以将数组更改为如下所示:

$ban = (object) array('type'=> 'mute', 'reason' => 'asdasd', 'curr_ts' => 1614661389, 'timestamp' => 1614751450, 'bannedby' => 'shoyeb');

var_dump($ban->type); // string(4) "mute"
var_dump($ban->reason); //  string(6) "asdasd" 
var_dump($ban->curr_ts); // int(1614661389) 
var_dump($ban->timestamp); // int(1614751450) 
var_dump($ban->bannedby); // string(6) "shoyeb"

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

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