PHP object like array

2020-02-10 01:53发布

I need to be able to set my object like this:

$obj->foo = 'bar';

then after that is set i need the following to be true

if($obj['foo'] == 'bar'){
  //more code here
}

标签: php arrays
9条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-10 02:07

You're mixing objects and arrays. You can create and access an object like so:

$obj = new stdClass;
$obj->foo = 'bar';

if($obj->foo == 'bar'){
// true
}

and an array like so:

$obj = new Array();
$obj['foo'] = 'bar';

if($obj['foo'] == 'bar'){
// true
}

You can define a class and add implements ArrayAccess if you want to access your class as both an array and a class.

http://www.php.net/manual/en/language.oop5.php

查看更多
小情绪 Triste *
3楼-- · 2020-02-10 02:07

You could also cast the object as an array:

if((array)$obj['foo'] == 'bar'){
  //more code here
}
查看更多
男人必须洒脱
4楼-- · 2020-02-10 02:08

Try extending ArrayObject

You'll also need to implement a __get Magic Method as Valentin Golev mentioned.

Your class will need to looks something like this:

Class myClass extends ArrayObject {
    // class property definitions...
    public function __construct()
    {
        //Do Stuff
    }

    public function __get($n) { return $this[$n]; }

    // Other methods
}
查看更多
不美不萌又怎样
5楼-- · 2020-02-10 02:11

Just add implements ArrayAccess to your class and add the required methods:

  • public function offsetExists($offset)
  • public function offsetGet($offset)
  • public function offsetSet($offset, $value)
  • public function offsetUnset($offset)

See http://php.net/manual/en/class.arrayaccess.php

查看更多
对你真心纯属浪费
6楼-- · 2020-02-10 02:11

ArrayObject implements the ArrayAccess interface (and some more). Using the ARRAY_AS_PROPS flag it provides the functionality you're looking for.

$obj = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
$obj->foo = 'bar';
echo $obj['foo'];

Alternatively you can implement the ArrayAccess interface in one of your own classes:

class Foo implements ArrayAccess {
  public function offsetExists($offset) {
    return isset($this->$offset);
  }

  public function offsetGet($offset) {
    return $this->$offset;
  }

  public function offsetSet($offset , $value) {
    $this->$offset = $value;
  }

  public function offsetUnset($offset) {
    unset($this->$offset);
  }
}

$obj = new Foo;
$obj->foo = 'bar';
echo $obj['foo'];
查看更多
forever°为你锁心
7楼-- · 2020-02-10 02:16

Enhance Class capability with no functionality drawbacks

You can also use ArrayAccess to access a single array property in your class and leave other properties being accessed in OOP way. Yet still it will work as you requested.

class Foo implements \ArrayAccess
{

/**
* mixed[] now you can access this array using your object 
* like a normal array Foo['something'] = 'blablabla'; echo  Foo['something']; ... and so on
* other properties will remain accessed as normal: $Foo->getName();
*/
private myArrayOptions = [];

private $name = 'lala';

    ...

    public function offsetExists($offset)
    {
        return isset($this->myArrayOptions[$offset]);
    }

    public function offsetGet($offset)
    {
        if ($this->offsetExists($offset)) {
            return $this->myArrayOptions[$offset];
        }

        return null; // or throw the exception;
    }

    public function offsetSet($offset, $value)
    {
        $this->myArrayOptions[$offset] = $value;
    }

    public function offsetUnset($offset)
    {
        unset($this->myArrayOptions[$offset]);
    }

    public function getName()
    {
        return $this->name;
    }

    public function __set($offset, $value){
        $this->myArrayOptions[$offset] = $value;
    }

    ...

}

The above will work as you expected.

$obj->foo = 'bar';
if($obj['foo'] == 'bar'){
    echo "WoWo";
}

Also note that Foo['name'] !== Foo->getName() those a two different variables

查看更多
登录 后发表回答