clouding/has-attributes

A trait to give class attributes

Maintainers

👁 cloudingcity

Package info

github.com/cloudingcity/has-attributes

pkg:composer/clouding/has-attributes

Statistics

Installs: 29

Dependents: 0

Suggesters: 0

Stars: 6

Open Issues: 0

v0.1.1 2019-03-25 07:00 UTC

Requires

  • php: >=7.2

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 86e84fc9a3d676b7b8ec43d497218f0a0bada5dd

  • Clouding <ghost0436.woop@gmail.com>

attributetrait

This package is auto-updated.

Last update: 2026-06-08 04:22:13 UTC


README

👁 Image
👁 Image
👁 Image
👁 Image

A trait to give class attributes

Features

  • Give class attributes with array.
  • Define attributes strict type.

Quick Example

Just use HasAttributes trait and pass key value array to constructor

class Post
{
 use \Clouding\HasAttributes\HasAttributes;
}

$post = new Post([
 'title' => 'Hello',
 'body' => 'World',
]);

echo $post->title; // Hello
echo $post->body; // World

Installation

composer require clouding/has-attributes

Usage

Define type

Declare $define to define type

require 'vendor/autoload.php';

interface Eatable {}

class Pig implements Eatable { }

class Zoo
{
 use \Clouding\HasAttributes\HasAttributes;

 protected $define = [
 'name' => 'string',
 'number' => 'int',
 'animal' => Eatable::class,
 ];
}

$zoo = new Zoo([
 'name' => 'Mike',
 'number' => 100,
 'animal' => new Pig(),
]);

echo $zoo->name; // Mike
echo $zoo->number; // 100
echo get_class($zoo->animal); // Pig

If you declare $define property, it will check type strictly

new Zoo(['name' => 999]);

// InvalidArgumentException: [name => 999] value is not equal to define type [string] 

And can't set key that is not defined

new Zoo(['foo' => 'bar']);

// InvalidArgumentException: Key [foo] is not defined 

Supported $define type:

  • string
  • int, integer
  • bool, boolean
  • object
  • array
  • real, float, double
  • Class, Interface

Set attributes

There have many way to set attributes

class Person
{
 use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
 'id' => 100,
]);

$person->setAttributes([
 'name' => 'Marry',
 'phone' => '0912345678'
]);

$person->setAttribute('sex', 'female');

$person->age = 18;

echo $person->id; // 100
echo $person->name; // Marry
echo $person->phone; // 0912345678
echo $person->sex; // female
echo $person->age; // 18

Get attributes

class Person
{
 use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
 'id' => 100,
 'name' => 'Jack',
]);

echo $person->getAttribute('id'); // 100

echo $person->getAttribute('salary', 0); // 0 (if key not exists return default value)

var_dump($person->getAttributes('id', 'name')); // ['id' => 100, 'name' => 'Jack']

var_dump($person->getAttributes(['id', 'name'])); // ['id' => 100, 'name' => 'Jack']