lukaszmakuch/object-attribute-container

允许将一些属性附加到对象上

v0.1 2016-03-10 19:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:18:34 UTC


README

允许将属性与对象关联。

获取包

$ composer require lukaszmakuch/object-attribute-container

使用对象属性容器

<?php

use lukaszmakuch\ObjectAttributeContainer\ObjectAttributeContainer;
use lukaszmakuch\ObjectAttributeContainer\Exception\AttributeNotFound;
use lukaszmakuch\ObjectAttributeContainer\Exception\ImpossibleToAddAttributes;

/* @var $attrContainer \ObjectAttributeContainer */

//create a test object
$obj = new \stdClass();

//associate parameters with the object
try {
    $attrContainer->addObjAttrs(
        $obj,
        ["attr-key" => "attr-val"]
    );
} catch (ImpossibleToAddAttributes $e) {
    //...
}

//check if there's a parameter with the given name
$attrContainer->objHasAttr($obj, "attr-key"); //true

//get that parameter value
try {
    $attrContainer->getObjAttrVal($obj, "attr-key"); //attr-val
} catch (AttributeNotFound $e) {
    //...
}

//remove that parameter
try {
    $attrContainer->remObjAttr($obj, "attr-key");
} catch (AttributeNotFound $e) {
    //...
}

获取容器实例

<?php

use lukaszmakuch\ObjectAttributeContainer\Impl\ObjectAttributeContainerImpl;
use lukaszmakuch\ObjectAttributeContainer\Impl\ObjectAttributeContainerProxy;

//default
$container = new ObjectAttributeContainerImpl();

//proxy
$containerProxy = new ObjectAttributeContainerProxy();

更多信息

更多详细信息请查看接口和测试用例。