DEV Community

Discussion on: The single most usefull php trait I use in my projects

Collapse
 
sadick profile image
Sadick

Yah sure, but the downside of implementing JsonSerializable is that i will need a different return value to jsonSerialize in all of my models because i have to construct an array representation of the class every time.

class Person implements \JsonSerializable
{
    private $id;
    private $name;
    public function setName($name)
    {
        $this->name = $name;
    }
    public function jsonSerialize()
    {
        return [
            'id' => $this->$id,
            'name' => $this->name,
        ];
    }
}
Collapse
 
daniel15 profile image
Daniel Lo Nigro

Good point. I forgot that traits can't implement interfaces in PHP. I use Hack at work, which is based on PHP but traits are able to implement interfaces when using Hack.