search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Home PHP Libraries Other libraries PHP library for dependency injection containers
PHP library for dependency injection containers
<?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.


Disclaimer

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

Implementing inter-class collaboration in PHP: best practices for dependency injection and interface abstraction Implementing inter-class collaboration in PHP: best practices for dependency injection and interface abstraction

04 Apr 2026

This article explains in detail how to avoid inheritance chain abuse in PHP, decouple collaboration between classes through dependency injection and interface abstraction, and allow the Checkout class to call the Mailer function in a safe and maintainable way, replacing erroneous cross-subclass method calls or hard-coded inheritance dependencies.

How to enable Pico.css on demand in a project without polluting styles globally How to enable Pico.css on demand in a project without polluting styles globally

09 Feb 2026

Pico.css has provided the pico.conditional.min.css version since v2, which only takes effect on elements with class="pico" added, completely solving the problem of style conflicts with other UI libraries.

What is the difference between mysql full database recovery and single database recovery_mysql operation method instructions What is the difference between mysql full database recovery and single database recovery_mysql operation method instructions

09 Feb 2026

Full database recovery is to directly import the SQL file generated by mysqldump--all-databases, covering all libraries (including system libraries). The risk is high, but it is suitable for the entire instance crash; you must check the CREATEDATABASE statement, target library list and character set, SQL mode, permission library synchronization and other details.

How to secure your MySQL server? (Security Hardening Checklist) How to secure your MySQL server? (Security Hardening Checklist)

26 Feb 2026

MySQL security hardening requires disabling anonymous users and test libraries, restricting root remote login and creating dedicated management accounts, forcing TLS encryption, closing local_infile and other dangerous functions, and regularly checking key security variables.

How to use the filter_var function for validation in PHP? How to use the filter_var function for validation in PHP?

09 Nov 2025

The filter_var() function is used to verify and clean data. It supports email, integer, URL, IP and other format verification. It is implemented through built-in filters such as FILTER_VALIDATE_EMAIL without the need for external libraries.

How to correctly load JARs with embedded dependencies (such as 'fat JARs') in Java applications How to correctly load JARs with embedded dependencies (such as 'fat JARs') in Java applications

10 Feb 2026

This article explains in detail why the "fat JAR" containing the libs/ directory cannot be directly loaded as a normal dependency, and the standardized practice plan for safely introducing external libraries in SpringBoot, Tomcat and other environments.

Show More