can i use stdClass like an array?

2020-04-30 07:09发布

is it possible to make stdClass objects work like a generically indexed array?

i.e. $array = Array ( [0] => 120 [1] => 382 [2] => 552 [3] => 595 [4] => 616 )

would be constructed like

$a = array();
$array[] = 120;
$array[] = 382;

etc.

but if i do that with an object it just overwrites itself:

$obj = new stdClass;
$obj->a = 120;
$obj->a = 382;

i know i can change 'a' every time,

sorry if this is a stupid question but it's stumping me for some reason!

Appreciate any help :)

Dan

6条回答
爷的心禁止访问
2楼-- · 2020-04-30 07:38

You can get exactly this in JavaScript.

查看更多
放我归山
3楼-- · 2020-04-30 07:42

An object is not an array. If you want numerically indexed elements, use an array.

If you want named elements use either an Object or an Associative Array.

As for why you're getting different behaviour, it's because in the Array, you are not specifying an index, so PHP uses the length of the Array as the index. With the Object, you have to specify the name (in this case 'a').

The other thing you may want to do is have an Object, with a member that is an array:

$obj = new stdClass;
$obj->arr = array();
$obj->arr[] = 'foo';
$obj->arr[] = 'bar';

also don't forget you can cast an array to an object:

$obj = (object) array(
    'arr' => array(
        'foo',
        'bar'
    )
);
查看更多
冷血范
4楼-- · 2020-04-30 07:45

No, you can't. Using the brackets ([]) like that is called "ArrayAccess" in PHP, and is not implemented on the stdClass object.

It sounds like you might want something like

$foo = new stdClass();
$foo->items = array();
$foo->items[] = 'abc';
$foo->items[] = '123';
$foo->items[] = 'you and me';

You could also try casting an array as a stdClass to see what happens.

$foo = array();
$foo[] = 'abc';
$foo[] = '123';
$foo[] = 'you and me';
$foo = (object) $foo;
var_dump($foo);
查看更多
Deceive 欺骗
5楼-- · 2020-04-30 07:48

I can't really see what you mean to do, short of

<?php
$obj->a = array();
$obj->a[] = 120;
$obj->a[] = 382;
// ...
?>

You cannot simply "push" a field onto an object. You need to know the name of the field in order to retrieve the value anyways, so it's pretty much nonsense.

查看更多
ら.Afraid
6楼-- · 2020-04-30 07:57

In short, no, because you will always have to name your properties. Going through the trouble of writing a simple class with ArrayAccess makes no sense either as you've essentially recreated an array with nothing to show for it except extreme sacrifices in performance and transparency.

Is there some reason your problem cannot be solved infinitely more simply by using an array, or is this just a wish-I-knew question?

EDIT: Check out this question for more info.

查看更多
男人必须洒脱
7楼-- · 2020-04-30 08:05

Only for the sake of OOP, don't use classes. If you really want to implement this functionality, use this class:

class ArrayClass
{
    protected $storage = array();

    public function push($content) {
        $this->storage[] = $content;
        return $this;
    }

    public function get($index) {
        if (isset($this->storage[$index])) {
            return $this->storage[$index];
        } else {
            //throw an error or
            return false;
        }
    }
}
查看更多
登录 后发表回答