hisorange / traits
该包已被弃用且不再维护。未建议替代包。
有用的PHP特性。
1.0.0
2014-05-01 21:28 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2021-06-13 21:35:24 UTC
README
HiSoRange - PHP Traits v1.0.0
一些有用的特性集合,我在我的包中使用,但也可能对任何了解它的人有用。
RunTimeCache 特性
简单的运行时缓存,用于结果或对象。特性保留 $runTimeCache 变量。
// Check if the key stored in the cache. $object->runTimeCacheExists('my.last.result.for.X'); // Store a value in the cache by key. $object->runTimeCacheSet('my.last.result.for.X', [1,2,3]); // Get the value from the cache by key. $object->runTimeCacheGet('my.last.result.for.Y', ['Fallback', 'value', 'if not found']); // Delete a value from the cache. $object->runTimeCacheDelete('resultX'); // Import a cache array, replace all existing value. $object->runTimeCacheImport(['r1' => 2, 'r3' => 94]); // Export the cache array. $object->runTimeCacheExport(); // Erase the cache values. $object->runTimeCacheReset();
ObjetConfig 特性
在对象上存储配置。特性保留 $objectConfig 变量。
// Check key in the config. $object->objectConfigExists('cache_key'); // Set a value in the config. $object->objectConfigSet('cache_key', 'hoc'); // Get a value from the config. $object->objectConfigGet('cache_key', 'fallback_value'); // Delete a configuration by key from the object. $object->objectConfigDelete('cache_key'); // Import a config array. $object->objectConfigImport($myNewConfig); // Export the config array. $object->objectConfigExport(); // Reset the config. $object->objectConfigReset();
PluginCollection 特性
将插件注册和注销到对象。特性保留 $pluginCollection 变量。
// Import plugins in PLUGIN => CONFIG setup. $object->pluginCollectionImport([ 'plugins\GoogleSearch' => ['secret_key' => 42], 'plugins\FacebookSearch' => ['secret_key' => 43], ]); // Export the collection. $object->pluginCollectionExport(); // Register a new plugin to the collection. $object->pluginCollectionRegister('plugins\AOL', ['secret_key' => 42]); // Deregister a plugin by it's key. $object->pluginCollectionDeregister('plugins\FacebookSearch'); // Check if a plugin is registered by it's key. $object->pluginCollectionIsRegistered('plugins\FacebookSearch'); // Practical useage. $this->pluginCollectionRegister('plugins\Bing', ['secret_key' => 95]); foreach($this->pluginCollectionExport() as $plugin => $config) { $pluginInstance = new $plugin; $pluginInstance->objectConfigImport($config); $plugin->searchOrWhatEver(); }