### Using Dice::CHAIN_CALL with Factories Source: https://github.com/level-2/dice/blob/master/README.md This example shows how `Dice::CHAIN_CALL` can be used with factory patterns. Dice is configured to instantiate `DatabaseFactory` and then call its `get` method with 'Database' as an argument, effectively creating and retrieving a database instance through the factory. The commented code shows the equivalent manual instantiation. ```PHP $dice = $dice->addRule('MyDatabase', [ 'instanceOf' => 'DatabaseFactory', 'call' => [ ['get', ['Database'], Dice::CHAIN_CALL] ] ] ); $database = $dice->create('MyDatabase'); //Equivalent of: $factory = new DatabaseFactory(); $database = $factory->get('Database'); ``` -------------------------------- ### Updating Rules with Immutable Dice in PHP 4.0 Source: https://github.com/level-2/dice/blob/master/README.md This example illustrates the change in behavior for `addRule` and `addRules` methods in Dice 4.0. Unlike previous versions, these methods now return a *new* Dice instance with the updated rules, making the container immutable. Developers must reassign the returned instance to continue using the updated configuration. ```php // Pre-4.0 code: $dice->addRule('PDO', ['shared' => true]); $db = $dice->create('PDO'); ``` ```php // 4.0 code: $dice = $dice->addRule('PDO', ['shared' => true]); $db = $dice->create('PDO'); ``` -------------------------------- ### Deprecated Instance Key for Constructor Parameters (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This code demonstrates the old, now deprecated, syntax for specifying named instances within constructParams using the literal string 'instance' as a key. This approach was problematic as it prevented passing arrays that coincidentally contained an 'instance' key. ```php $dice->addRule('ClassName', [ 'constructParams' => ['instance' => '$NamedPDOInstance'] ]); ``` -------------------------------- ### Using Dice::INSTANCE for Named Constructor Parameters (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This snippet illustrates the new, recommended way to specify named instances in constructParams using the \Dice\Dice::INSTANCE constant. This change resolves the issue where arrays with an 'instance' key could not be passed as constructor parameters. ```php $dice->addRule('ClassName', [ 'constructParams' => [\Dice\Dice::INSTANCE => '$NamedPDOInstance'] ]); ``` -------------------------------- ### Basic Object Creation with Dice in PHP Source: https://github.com/level-2/dice/blob/master/README.md This snippet demonstrates the fundamental usage of the Dice PHP Dependency Injection Container. It shows how to define classes with dependencies (Class A depends on Class B) and then use Dice to automatically resolve and inject these dependencies when creating an instance of Class A, requiring no explicit configuration. ```php b = $b; } } class B { } require_once 'Dice.php'; $dice = new \Dice\Dice; $a = $dice->create('A'); var_dump($a->b); //B object ?> ``` -------------------------------- ### Configuring Router and PDO with Constants and Superglobals (JSON) Source: https://github.com/level-2/dice/blob/master/README.md This JSON snippet demonstrates how to define Dice rules in an external file, allowing the injection of PHP superglobals like $_SERVER using Dice::GLOBAL and PHP constants like PDO::ATTR_ERRMODE using Dice::CONSTANT into constructor parameters or method calls. ```json { "Router": { "constructParams": [ {"Dice::GLOBAL": "_SERVER"} ] }, "PDO": { "shared": true, "constructParams": [ "mysql:dbname=testdb;host=127.0.0.1", "dbuser", "dbpass" ], "call": [ [ "setAttribute", [ {"Dice::CONSTANT": "PDO::ATTR_ERRMODE"}, {"Dice::CONSTANT": "PDO::ERRMODE_EXCEPTION"} ] ] ] } } ``` -------------------------------- ### Demonstrating PHP Object Method Chaining Source: https://github.com/level-2/dice/blob/master/README.md This snippet illustrates a common pattern of object method chaining in PHP, where multiple methods are called sequentially on the same object instance, returning the modified object each time. This is a prerequisite for understanding Dice's `CHAIN_CALL` feature. ```PHP $httpRequest = new HTTPRequest(); $httpRequest = $httpRequest->url('http://example.org')->method('POST')->postdata('foo=bar'); ``` -------------------------------- ### Wrapping Closures with 'instance' for Dice Rules (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This snippet demonstrates the updated and required method for passing closures as values within Dice rules. Closures must now be wrapped in an array with the key 'instance' (e.g., ['instance' => function() {...}]) to prevent their immediate execution and allow them to be passed as actual callable values. ```php $rule->substitutions['A'] = ['instance' => function() { return new A; }]; $rule->call[] = ['someMethod', ['instance' => function() { // '2' will be provided as the first argument when someMethod is called return 2; }]]; $rule->constructParams[] = ['instance' => function() { //'abc' will be providedas the first constructor parameter return 'abc'; }]; ``` -------------------------------- ### Configuring Dice for Object Method Chaining (Dice::CHAIN_CALL) Source: https://github.com/level-2/dice/blob/master/README.md This code demonstrates how to configure Dice to support object method chaining using the `call` rule and `Dice::CHAIN_CALL`. Dice will execute the specified methods (`url`, `method`, `postdata`) on the `HTTPRequest` object, passing the results of each call to the next, effectively replacing the initial object with the final chained result. ```PHP $dice = $dice->addRule('HTTPRequest', ['call' => [ ['url', ['http://example.org'], Dice::CHAIN_CALL], ['method', ['POST'], Dice::CHAIN_CALL ], ['postdata', ['foo=bar'], Dice::CHAIN_CALL] ] ] ); ``` -------------------------------- ### Loading Dice Rules from JSON File Source: https://github.com/level-2/dice/blob/master/README.md This code shows how to load Dice rules from an external JSON file using the `addRules` method. It reads the content of 'rules.json', decodes it from JSON into a PHP array, and then passes this array to `addRules` for bulk rule configuration. ```PHP $dice->addRules(json_decode(file_get_contents('rules.json'))); ``` -------------------------------- ### Adding Multiple Dice Rules with addRules Method Source: https://github.com/level-2/dice/blob/master/README.md This snippet demonstrates the `addRules` method, which allows adding multiple Dice rules simultaneously using an associative array. It configures `\PDO` to be shared and `Framework\Router` to be constructed with specific parameters, replacing the need for a separate JSON loader. ```PHP $dice->addRules([ '\PDO' => [ 'shared' => true ], 'Framework\Router' => [ 'constructParams' => ['Foo', 'Bar'] ] ]); ``` -------------------------------- ### Migrating Dice Class Names to Namespaced PHP Source: https://github.com/level-2/dice/blob/master/README.md This snippet provides essential find/replace patterns to update old Dice class instantiations to their new, namespaced equivalents. These changes are required for backward compatibility after the introduction of basic namespace support in the Dice library, ensuring applications continue to function correctly. ```PHP new Dice => new \Dice\Dice new DiceInstance => new \Dice\Instance new DiceRule => new \Dice\Rule ``` -------------------------------- ### Named Instance Rule Inheritance (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This PHP code demonstrates the updated behavior where named instances, defined using instanceOf, now automatically inherit rules from the class they are instances of. This means if MyClass is shared, $MyNamedInstance will also be shared, combining rules from both definitions. ```php $rule = []; $rule['shared'] = true; $dice->addRule('MyClass', $rule); $rule = []; $rule['instanceOf'] = 'MyClass'; $rule['constructParams'] = ['Foo', 'Bar']; $dice->addRule('$MyNamedInstance', $rule); ``` -------------------------------- ### Loading External JSON Rules into Dice (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This PHP snippet shows the method for integrating rules defined in an external JSON file into the Dice container. It uses file_get_contents to read the JSON file and json_decode to parse it into a PHP array, which is then passed to the $dice->addRules() method. ```php $dice->addRules(json_decode(file_get_contents('rules.json'))); ``` -------------------------------- ### Deprecated Direct Closure Execution in Dice Rules (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This code illustrates the previous Dice behavior where closures passed directly as substitutions, call arguments, or constructor parameters were immediately executed. This made it impossible to pass a closure itself as a value, as it would always return its result. ```php $rule->substitutions['A'] = function() { return new A; }; $rule->call[] = ['someMethod', function() { // '2' will be provided as the first argument when someMethod is called return 2; }]; $rule->constructParams[] = function() { //'abc' will be providedas the first constructor parameter return 'abc'; }; ``` -------------------------------- ### Preventing Named Instance Rule Inheritance (PHP) Source: https://github.com/level-2/dice/blob/master/README.md This snippet shows how to revert to the previous behavior for named instances by explicitly setting $rule['inherit'] = false;. This prevents the named instance from inheriting rules from the class specified in instanceOf, ensuring only its own defined rules are applied. ```php $rule = []; $rule['shared'] = true; $dice->addRule('MyClass', $rule); $rule = []; $rule['instanceOf'] = 'MyClass'; $rule['constructParams'] = ['Foo', 'Bar']; //Prevent the named instance inheriting rules from the class named in `instanceOf`: $rule['inherit'] = false; $dice->addRule('$MyNamedInstance', $rule); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.