# How to use Trait in laminas

**URL:** <https://discourse.laminas.dev/t/how-to-use-trait-in-laminas/2055>\
**Category:** Components & MVC\
**Tags:** laminas-mvc\
**Created:** [January 20, 2021, 4:14am UTC](https://discourse.laminas.dev/t/how-to-use-trait-in-laminas/2055 "2021-01-20T04:14:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nghiata90](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nghiata90/32/859_2.png) [@nghiata90](https://discourse.laminas.dev/u/nghiata90)\
**Post date:** [January 20, 2021, 4:14am UTC](https://discourse.laminas.dev/t/how-to-use-trait-in-laminas/2055/1 "2021-01-20T04:14:22Z")

</div>

I would like to create a standard JsonModel format. Furthermore, I think I will implement a trait and use it in a controller. However, I couldn’t find a way to using trait in Laminas. How do you solve the issues? Should I use trait or have another way better?

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [January 20, 2021, 8:09am UTC](https://discourse.laminas.dev/t/how-to-use-trait-in-laminas/2055/2 "2021-01-20T08:09:18Z")

</div>

Well, there is a huge discussion about traits in PHP anyway. It seems that most developers tend to avoid traits for a good reason. If you 're interested in this discussion, have a look at this discussion on twitter: [https://twitter.com/medunes/status/1351563554340696070](https://twitter.com/medunes/status/1351563554340696070).

Anyway, you can use Traits in Laminas at any time. Here 's a short example using the `JsonSerializable` interface and the method `jsonSerialize` as a trait.

```
<?php
declare(strict_types=1);
namespace Application\Entity\Trait;

trait JsonTrait
{
    public function jsonSerialize()
    {
        $data = get_object_vars($this);
        return json_encode($data);
    }
}

```

As you can see this is a simple trait for JSON encoding object properties. You must use the normal namespacing rules for the PSR-4 autoloading in laminas. Once created a trait, you can use it like all other stuff in laminas.

```
<?php
declare(strict_types=1);
namespace Application\Entity;

use Application\Entity\Trait\JsonTrait;
use JsonSerializable;

class MyNameEntity implements JsonSerializable
{
    use JsonTrait;

    protected string $name = '';

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

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

```

As you can see this is a simple value object containing the a name property and implementing the `JsonSerializable` interface. Beside that we 're using our `JsonTrait`. Just implement it by using the standard `use` statement before listing the class properties.

Let 's use this …

```
$entity = new Application\Entity\MyNameEntity();
$entity->setName('Marcel');
$json = json_encode($entity);

```

The above shown example ends up with …

```
{"name":"Marcel"}

```

This example demonstrate the basic usage of traits. If you use traits you should decide on your own.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 20, 2021, 11:03am UTC](https://discourse.laminas.dev/t/how-to-use-trait-in-laminas/2055/3 "2021-01-20T11:03:54Z")

</div>

> [@nghiata90](#):
>
> I would like to create a standard JsonModel format.

You can use [`Laminas\View\Model\JsonModel`](https://github.com/laminas/laminas-view/blob/c34b15f42cac410f35010163de169ab71c39bf73/src/Model/JsonModel.php#L15) in your controller. Internally, the model uses laminas-json which supports the [`JsonSerializable` interface](https://www.php.net/manual/en/class.jsonserializable.php) and the [methods `toJson` and `toArray`](https://docs.laminas.dev/laminas-json/advanced/#encoding-php-objects) of your objects.

In the quick start of laminas-view you will find an example:

[https://docs.laminas.dev/laminas-view/quick-start/#creating-and-registering-alternate-rendering-and-response-strategies](https://docs.laminas.dev/laminas-view/quick-start/#creating-and-registering-alternate-rendering-and-response-strategies)
