rakshazi/get-set-trait

适用于PHP 5.4+的动态setter-getter

1.0.5 2019-09-24 08:54 UTC

This package is auto-updated.

Last update: 2024-09-24 20:57:41 UTC


README

A dynamic setter-getter library for PHP 5.4+.

You can use methods like setFoo('bar') and getFoo(), which you DON'T have to create (in your class). GetSetTrait will make these methods work for you automatically as long as you have a $foo property in your class.

It makes use of Traits, so using it is super simple, you don't have to extend any class, as you can extend a single class only, we don't force you to use ours. You can restrict to only getter only or you can specify a Type for property using annotations.

安装

GetSetTrait 使用 Composer 来简化流程。

学习如何使用Composer,并将其添加到require中(在您的composer.json文件中)

"rakshazi/get-set-trait": "@stable"

Packagist 上的库。

用法

只需将此添加到您的类中

use Rakshazi\GetSetTrait;

class Dummy
{
    //Add ability to use dynamic getters and setters
    use \Rakshazi\GetSetTrait;
}

//Init dummy class
$dummy = new Dummy;
//Set new var 'message_for_world' with value
$dummy->setMessageForWorld('Hello');
//Will return "Hello\n"
echo $dummy->getMessageForWorld()."\n";
//Will return the same text
echo $dummy->getData('message_for_world')."\n";
//Set new message for our world!
$dummy->setData('message_for_world', 'Bye-bye!');
//Will return "Bye-bye!\n"
echo $dummy->getData('message_for_world')."\n";
//Will set new var 'new_message'
$dummy->setNewMessage('Use me now!');
//Will return true, value exists
$dummy->hasNewMessage();
//Will return false
$dummy->hasSomeOtherValue();
//Will show all object data
var_dump($dummy);

基本上就是这样。

高级用法

数据属性

If you want save all data in $object->someProperty array instead of saving each property as object's property ($object->property_name), you can use setDataProperty('data') function, example

<?php
class Dummy
{
    //Add ability to use dynamic getters and setters
    use \Rakshazi\GetSetTrait;

    public function __construct()
    {
        $this->setDataProperty('data');
    }
}

//Init dummy class
$dummy = new Dummy;
//Set new var 'message_for_world' with value
$dummy->setMessageForWorld('Hello');
//Will return "Hello\n"
echo $dummy->getMessageForWorld()."\n";
//Will return the same text
echo $dummy->getData('message_for_world')."\n";
//Set new message for our world!
$dummy->setData('message_for_world', 'Bye-bye!');
//Will return "Bye-bye!\n"
echo $dummy->getData('message_for_world')."\n";
//Will set new var 'new_message'
$dummy->setNewMessage('Use me now!');
//Will return all properties in an array
$dummy->getAllData();
//Will show all object data
var_dump($dummy);

结果(所有数据保存在 data 属性中)

object(Dummy)#1 (2) {
  ["_data_property":"Dummy":private]=>
  string(4) "data"
  ["data"]=>
  array(2) {
    ["message_for_world"]=>
    string(8) "Bye-bye!"
    ["new_message"]=>
    string(11) "Use me now!"
  }
}