PHP code example of drupol / dynamicobjects

1. Go to this page and download the library: Download drupol/dynamicobjects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

drupol / dynamicobjects example snippets




onymous 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!



rupol\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!



rupol\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();

/**
 * 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()