PHP Arrayable Interface

gawpertron picture gawpertron · Aug 7, 2012 · Viewed 20.9k times · Source

I'm sure I read a while back about a new feature of PHP that was either a new magic method or a new interface so that you could implement Arrayable methods.

eg

interface Arrayable
{
    public function toArray();
}

Was I imagining it?

Answer

coatesap picture coatesap · Mar 20, 2014

It's not in PHP itself, but Laravel has an interface that is intended for that exact purpose:

<?php namespace Illuminate\Contracts\Support;

interface Arrayable {

    /**
     * Get the instance as an array.
     *
     * @return array
     */
     public function toArray();

}

Note: In Laravel v4 the namespace was Illuminate\Support\Contracts and the interface name was ArrayableInterface.