是否可以将类对象视为变量?

问题描述:

Is it possible to treat class object like a variable ???

What i know we can treat it like a function:

class hello{
    public function __invoke(){
        return ['one','two','three'];
    }
}

$obj = new hello;
var_export($obj()); //returns the defined array ['one','two','three']

What i need to do is to do it with out the (): means treat it like a variable & make it return an (array or another object)

$obj = new hello;
var_export($obj); //returns the defined array ['one','two','three']

Is there any magic method like __invoke() to do this... or even a hacky way to do it ???

是否可以将类对象视为变量? p>

我知道我们可以像对待函数一样对待它: p>

  class hello {
 public function __invoke(){
 return  ['one','two','three']; 
} 
} 
 
 $ obj = new hello; 
var_export($ obj());  //返回定义的数组['one','two','three'] 
  code>  pre> 
 
 

我需要做的就是用()来做 : means将它视为一个变量& 让它返回一个(数组或另一个对象) p>

  $ obj = new hello; 
var_export($ obj);  //返回已定义的数组['one','two','three'] 
  code>  pre> 
 
 

是否有任何魔术方法,如 __ invoke() 代码>做这个...甚至是一个hacky方式做到这一点??? p> div>

No, it's not possible to do that because one can't extent built-in things like array. There are some ways to achieve parts of what you want though:

Printing out custom data on var_dump()

This is a feature was introduced in PHP 5.6 with the __debugInfo() magic method.

class Hello {
    public function __debugInfo(){
        return ['one','two','three'];
    }
}

var_dump(new Hello);

This would output:

object(Hello)#1 (3) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(5) "three"
}

Acting like an array

While you can't make your objects be an array (that is, extend it), they can behave like arrays if you implement the ArrayAccess interface:

class Hello implements ArrayAccess {
    private $data = [];
    public function offsetExists($offset) {
        return isset($this->data[$offset]);
    }
    /* insert the rest of the implementation here */
}

And then you can use it like an array:

$fake_array = new Hello();
$fake_array['foo'] = 'bar';
echo $fake_array['foo'];

Note that you can't pass classes that implement this interface into methods hinted with array.


It is not possible to act like any other primitive data type unfortunately. If you want ultimate flexibility, you will have to look at things like Python and Scala. In PHP you will need to use some pattern like a getData() and setData() interface for the wrapping object.

In addition to Anonymous's answer, along with \ArrayAccess it can be useful to implement \IteratorAggregate (to work with foreach()) and \Countable (to work with count())

namespace {
    abstract class AEnumerable implements \IteratorAggregate, \Countable, \ArrayAccess {
        protected $_array;

        public function getIterator() {
            return new \ArrayIterator($this->_array);
        }
        public function count() {
            return count($this->_array);
        }
        public function offsetExists( $offset ) {
            return isset($this->_array[$offset]);
        }
        public function offsetGet( $offset ) {
            return $this->_array[$offset];
        }
        public function offsetSet( $offset, $value ) {
            $this->_array[$offset] = $value;
        }
        public function offsetUnset( $offset ) {
            unset( $this->_array[$offset] );
        }
    }
}