tasoft/value-storage

v1.0.0 2020-04-21 18:43 UTC

This package is auto-updated.

Last update: 2024-09-22 04:34:48 UTC


README

这是一个简单的存储值和值提供者的存储库。

安装

$ composer require tasoft/value-storage

使用

值存储可以使用类似于简单数组的语法。
此外,它接受具有特定特性的值:它们可以在需要时解析。

<?php
use TASoft\Util\ValueStorage;

$vp = new ValueStorage(["value1" => 23]);
$vp->value2 = 44;
$vp["value3"] = 'Hello World';

// The values are fetchable by properties or array access:
echo $vp->value3; // 'Hello World'
echo $vp["value1"]; // 23

这种行为目前并不特别。
但看看这个

<?php
use TASoft\Util\ValueStorage;

$vp = new ValueStorage(["test" => $func1 = function() { return 23; }]);
// or
$vp["other-test"] = $func2 = function() { return "Hello World"; };

// You can assign a callback using the constructor, a property name or by array access.

// Fetching the value provider is done by then get() method
echo $func1 === $vp->get('test'); // TRUE
// or
echo $func1 === $vp->test; // TRUE

// And fetching the real value is done by getValue or array access
echo $vp->getValue('test'); // 23
// or
echo $vp["test"]; // 23 !

就像实现了 TASoft\Util\Value\ValueInterface 的回调对象一样工作。

传递实现了 TASoft\Util\Value\ValueInterfaces 的对象会注入该对象返回的值。您可以通过任何属性、键或简单地通过 addValue() 方法分配此类对象。