drupol/dynamicobjects

此包已废弃,不再维护。未建议替代包。

在PHP对象上创建和管理动态属性和方法。

1.4.3 2019-07-23 20:00 UTC

This package is auto-updated.

Last update: 2022-05-15 03:19:36 UTC


README

Build Status Scrutinizer Code Quality Code Coverage StyleCI Latest Stable Version Total Downloads License

DynamicObjects

描述

在PHP对象上创建和管理动态属性和方法。

特性

  • 允许您向现有对象添加常规或静态方法属性
  • 可以通过扩展DynamicObject对象或作为特性,在真实或匿名类中使用
  • 如果方法是可调用的,可以缓存方法结果和/或属性
  • 缓存对象使用CacheInterface并可以进行注入

要求

  • PHP >= 5.6

安装

composer require drupol/dynamicobjects

可选

要启用缓存,您需要额外的包。

composer require drupol/memoize

使用

使用对象

<?php

include 'vendor/autoload.php';

// Anonymous classes creation is only available to PHP >= 7.
$myObject = new class extends \drupol\DynamicObjects\DynamicObject {};

$myObject::addDynamicProperty('name', 'DynamicObjects');
echo $myObject->name; // DynamicObjects

$myObject::addDynamicMethod('sayHelloWorld', function() {echo "Hello world!";});
$myObject->sayHelloWorld(); // Hello world!

使用特性

<?php

include 'vendor/autoload.php';

use drupol\DynamicObjects\DynamicObjectsTrait;

// Anonymous classes creation is only available to PHP >= 7.
$myObject = new class {
    use DynamicObjectsTrait;
};

$myObject::addDynamicProperty('name', 'DynamicObjects');
echo $myObject->name; // DynamicObjects

$myObject::addDynamicMethod('sayHelloWorld', function() {echo "Hello world!";});
$myObject->sayHelloWorld(); // Hello world!

缓存

<?php

include 'vendor/autoload.php';

use drupol\DynamicObjects\DynamicObjectsTrait;

// Anonymous classes creation is only available to PHP >= 7.
$myObject = new class {
    use DynamicObjectsTrait;
};

$myObject::addDynamicMethod('sleep', function($second = 5) {
  sleep($second);
  return true; // The function must return something to get the memoization working.
  }, true); // Set the last parameter to true to enable the memoization.

$myObject->sleep(); // The first execution will be executed and will last 5 seconds.
$myObject->sleep(); // The next executions, if arguments and method are the same will not be executed
$myObject->sleep(); // and only the result of the function will be returned.
$myObject->sleep();

API

DynamicObjects提供了一个PHP特性和一个依赖于该特性的对象。

您可以通过扩展对象或直接使用特性来使用它,两者没有区别。

它公开了以下静态方法

/**
 * Add a dynamic property.
 *
 * @param string $name
 *   The property name.
 * @param mixed $value
 *   The property value.
 * @param bool $memoize
 *   Memoize parameter.
 */
DynamicObjectsTrait::addDynamicProperty($name, $value, $memoize = false);
/**
 * Add a dynamic method.
 *
 * @param $name
 *   The method name.
 * @param \Closure $func
 *   The method.
 * @param bool $memoize
 *   Memoize parameter.
 * @param bool $static
 *   Static flag parameter.
 */
DynamicObjectsTrait::addDynamicMethod($name, $func, $memoize = false, $static = false);
/**
 * Check if a dynamic property exists.
 *
 * @param string $name
 *   The property name.
 * @return bool
 *   True if the property exists, false otherwise.
 */
DynamicObjectsTrait::hasDynamicProperty($name);
/**
 * Check if a dynamic method exists.
 *
 * @param string $name
 *   The property name.
 * @return bool
 *   True if the property exists, false otherwise.
 */
DynamicObjectsTrait::hasDynamicMethod($name);
/**
 * Get a dynamic property.
 *
 * @param $name
 *   The property name.
 * @return mixed|null
 *   The property value if it exists, null otherwise.
 */
DynamicObjectsTrait::getDynamicProperty($name);
/**
 * Get a dynamic method.
 *
 * @param $name
 *   The method name.
 * @return mixed|null
 *   The method if it exists, null otherwise.
 */
DynamicObjectsTrait::getDynamicMethod($name);
/**
 * Remove a dynamic property.
 *
 * @param string $name
 *   The property name.
 */
DynamicObjectsTrait::removeDynamicProperty($name);
/**
 * Remove a dynamic method.
 *
 * @param string $name
 *   The method name.
 */
DynamicObjectsTrait::removeDynamicMethod($name);
/**
 * Clear dynamic properties.
 */
DynamicObjectsTrait::clearDynamicProperties();
/**
 * Clear dynamic methods.
 */
DynamicObjectsTrait::clearDynamicMethods();
/**
 * Set the cache.
 *
 * @param \Psr\SimpleCache\CacheInterface $cache
 */
DynamicObjectsTrait::setDynamicObjectCacheProvider(CacheInterface $cache);
/**
 * Get the cache.
 *
 * @return \Psr\SimpleCache\CacheInterface
 */
DynamicObjectsTrait::getDynamicObjectCacheProvider();
/**
 * Clear the cache.
 */
DynamicObjectsTrait::clearDynamicObjectCache()

贡献

欢迎通过发送Github pull requests的方式为此库做出贡献。我会非常积极地回应 :-)

赞助商