Overload the behavior of count() when called on certain objects [duplicate]

ぐ巨炮叔叔 提交于 2019-11-30 11:17:25

It sounds like you want to implement the Countable interface:

class a implements Countable {
    public function __construct() {
        $this->list = array("1", "2");
    }

    public function count() {
      return count($this->list);
    }
}

$blah = new a();

echo count($blah); // 2

tl;dr - answer is at the bottom :)

In PHP, this model is reversed; rather than overloading functions (not methods) with different argument types, each class is meant to define magic methods for each of those functions.

Below is the list of functions that magic behaviour can be defined for inside your class. In the examples, each reference to $obj is an instance of your class, ->unknown refers to a missing property and ->blamethod() refers to a missing method.

  1. __toString() - this method is called when your object is used in a string context, e.g. echo "My object is: $obj\n";

  2. __invoke([$arg1..n]) - when your object is used as a function, this method gets called, e.g. $obj($a, $b);

  3. __get($prop) - allows for intercepting an attempt to access a non-existing property of your class, e.g. $obj->unknown; btw, this can sometimes be used as a way to lazy load certain properties that would otherwise take a considerable amount of processing when done in the constructor.

  4. __set($prop, $value) - gets called when a non-existing property is being set, e.g. $obj->unknown = 42;

  5. __isset($prop) - called to determine the existence of a non-existing property (I realize how funny that sounds), e.g. isset($obj->unknown) would call $obj->__isset('unknown')

  6. __unset($prop) - called in cases like these unset($obj->unknown);

  7. __call($name, $arguments) - intercepts a call to an unimplemented method of your class, e.g. $obj->blamethod(1, 2, 3); will invoke $obj->__call('blamethod', array(1, 2, 3));

  8. __callStatic($name, $arguments) - like __call() but you will not be able to use $this inside your implementation.

  9. __clone() - called when $x = clone $obj; is called, so you get to decide what data is kept and what's thrown away.

With SPL, a few more concepts were introduced by means of implementing certain interfaces:

  1. Traversable - an abstract interface that defines what your class does when used in a foreach construct; the concrete interface is called Iterator.

  2. ArrayAccess - an interface that allows using instances of your class to be used like an array.

  3. Serializable - an interface that defines two methods to be called upon serialize() or unserialize(). It's mutually exclusive to using __sleep() and __wakeup().

  4. Countable - defines one method to be called whenever count() is performed on your class instance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!