PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / props-dic / scripts / pizza.php

pizza.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/mrclay/props-dic/scripts/pizza.php

56 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require __DIR__ . '/../vendor/autoload.php';
4
5 class Dough {}
6 class Cheese {}
7 class Slice {}
8 class Pizza {
9 function __construct($style, Cheese $cheese) {}
10 function setDough(Dough $dough) {}
11 function getSlice() { return new Slice(); }
12 }
13 class CheeseFactory {
14 static function getCheese() { return new Cheese(); }
15 }
16
17 /**
18 * @property-read string $style
19 * @property-read Dough $dough
20 * @property-read Cheese $cheese
21 * @property-read Pizza $pizza
22 * @method Slice new_slice()
23 */
24 class MyDI extends \Props\Container {
25 public function __construct() {
26 $this->style = 'deluxe';
27
28 $this->dough = function (MyContainer $c) {
29 return new Dough();
30 };
31
32 $this->setFactory('cheese', 'CheeseFactory::getCheese');
33
34 $this->pizza = function (MyContainer $c) {
35 $pizza = new Pizza($c->style, $c->cheese);
36 $pizza->setDough($c->dough);
37 return $pizza;
38 };
39
40 // note 3rd argument $shared is false
41 $this->slice = function (MyContainer $c) {
42 return $c->pizza->getSlice();
43 };
44 }
45 }
46
47 $c = new MyContainer;
48
49 // You can request dependencies in any order. They're resolved as needed.
50
51 $slice1 = $c->new_slice(); // This first resolves and caches the cheese, dough, and pizza.
52 $slice2 = $c->new_slice(); // This just gets a new slice from the existing pizza.
53
54 assert($slice1 !== $slice2);
55 assert($c->pizza === $c->pizza);
56