Tools For Better Developers

Osm Admin: Data Class Traits

2021 November Osm Admin

6 months ago ∙ 1 minute read

Different data classes have repeating structural patterns. For example, most data classes stored in database tables have the auto-incremented id property. Use PHP traits to effectively introduce the same properties to different data classes over and over again.

Osm Admin brings in several standard data class traits:

Id

Instead of defining id property manually, use Id trait:

use Osm\Admin\Base\Traits\Id;
...

/**
 * @property string $email #[Serialized]
 * @property string $password #[Serialized]
 */
#[Table('accounts')]
class Account extends Object_
{
    use Id;
}

The Id trait defines id property as follows:

/**
 * @property int $id #[
 *      Serialized,
 *      Table\Increments
 * ]
 */
trait Id
{

}