<?php namespace Auryn; class CachingReflector implements Reflector { const CACHE_KEY_CLASSES = 'auryn.refls.classes.'; const CACHE_KEY_CTORS = 'auryn.refls.ctors.'; const CACHE_KEY_CTOR_PARAMS = 'auryn.refls.ctor-params.'; const CACHE_KEY_FUNCS = 'auryn.refls.funcs.'; const CACHE_KEY_METHODS = 'auryn.refls.methods.'; private $reflector; private $cache; public function __construct(Reflector $reflector = null, ReflectionCache $cache = null) { $this->reflector = $reflector ?: new StandardReflector; $this->cache = $cache ?: new ReflectionCacheArray; } public function getClass($class) { $cacheKey = self::CACHE_KEY_CLASSES . strtolower($class); if (!$reflectionClass = $this->cache->fetch($cacheKey)) { $reflectionClass = new \ReflectionClass($class); $this->cache->store($cacheKey, $reflectionClass); } return $reflectionClass; }
Our idea is that when the application uses a Foo class, it will create the Foo class and call the methods of the Foo class. If this method requires a Bar class, it will create the Bar class and call the Bar class methods. This method requires a Bim class, it will create the Bim class, and then do other work. The idea of using dependency injection is that the application uses the Foo class, the Foo class needs the Bar class, and the Bar class needs the Bim class, then first create the Bim class, then create the Bar class and inject Bim, then create the Foo class, and inject the Bar class , then call the Foo method, Foo calls the Bar method, and then does other work. This is the Inversion of Control pattern. Control of dependencies is reversed to the beginning of the call chain. This way you have complete control over dependencies and control the behavior of your program by adjusting different injected objects. For example, the Foo class uses memcache, and you can use redis instead without modifying the Foo class code.
The idea behind using a dependency injection container is that if the application needs to get the Foo class, it gets the Foo class from the container, the container creates the Bim class, then creates the Bar class and injects Bim, then creates the Foo class, and injects it into the Bim class. Bar injection, the application calls the Foo method, Foo calls the Bar method, and then does other work. In short, the container is responsible for instantiation, injecting dependencies, processing dependencies, etc.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article

18Dec2024
Dependency Injection in Go: Exploring Alternative PatternsIn the code provided, the wiring of components in the main function manually passes a...

29Dec2024
What is Dependency Injection in PHP, and Why is it Important for Testing and Code Maintainability? Dependency Injection (DI) is a design pattern used in software development to improve code flexibility, testability, and maintainability. It is p

13Dec2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

22Dec2024
Organizational Patterns for Helper Objects in PHP ProjectsIntroductionIn PHP object-oriented development, managing and organizing helper objects...

28Oct2024
Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...

03Jan2025
Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...


Hot Tools

A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking

Small PHP library for optimizing images
Small PHP library for optimizing images

PHP function class for winning probability algorithm
PHP function class for winning probability algorithm
