Giter VIP home page Giter VIP logo

dependency-injection's Introduction

感谢关注「GitHub 热门」公众号,带你了解技术圈内热门新鲜事!

dependency-injection's People

Contributors

iidestiny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gaoyia greatxj

dependency-injection's Issues

Another simpler implementation

How about this way?

<?php

namespace App\Http\Controllers;

class ExampleController  extends Controller {

    /**
     * di helper
     */
    protected function make($cls) {
        return new class($cls) {
            use \Illuminate\Routing\RouteDependencyResolverTrait;
            public function __construct($cls)
            {
                $this->container = app();
                $this->ins = app($cls);
            }
            public function __call($name, $arguments)
            {
                $this->ins->$name(...$this->resolveClassMethodDependencies($arguments, $this->ins, $name));
            }
        };
    }


    /**
     * usage
     */
    public function store()
    {
        $this->make(OrderService::class)->placeOrder($user, $goods, $address);
    }
}

di_register实例化多个对象

例如

class Book
{
    public function say()
    {
        echo "book", PHP_EOL;
    }
}

class Pen
{
    public function say()
    {
        echo 'pen', PHP_EOL;
    }
}

$book = di_register(Book::class);
$pen = di_register(Pen::class);

$book->say();
$pen->say();

将会输出:

pen
pen

查看源码,发现原因出在'Iidestiny\DependencyInjection\App'第21行,

protected static $instance;

因为register方法每次返回一个新的App实例,但是instance却是静态变量

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.