Skip to content

[5.4] Container:Extend and Rebinding bug or feature? #19241

@LeoYang90

Description

@LeoYang90
  • Laravel Version: 5.4.23
  • PHP Version:7.1
  • Database Driver & Version:NA

Hi,
Recently, I am learning the source code of container, there is something that I can not figure out whether it is a bug or a feature:

Description:

This is a test for bind():

    public function testExtendReBinding()
    {
        $_SERVER['_test_rebind'] = false;

        $container = new Container;
        $container->rebinding('foo',function (){
            $_SERVER['_test_rebind'] = true;
        });
        $container->bind('foo',function (){
            $obj = new StdClass;

            return $obj;
        });

        $container->make('foo');

        $container->extend('foo', function ($obj, $container) {
            return $obj;
        });

        PHPUnit_Framework_Assert::assertFalse($_SERVER['_test_rebind']);
    }

If we use bind() function to bind abstract, the rebinding() will not be fired by extend() after make() function.
However,this is a test for instance():

    public function testExtendReBindingInstance()
    {
        $_SERVER['_test_rebind'] = false;

        $container = new Container;
        $container->rebinding('foo',function (){
            $_SERVER['_test_rebind'] = true;
        });

        $obj = new StdClass;
        $container->instance('foo',$obj);

        $container->make('foo');

        $container->extend('foo', function ($obj, $container) {
            return $obj;
        });

        PHPUnit_Framework_Assert::assertTrue($_SERVER['_test_rebind']);
    }

if we use instance() function to bind abstract, the rebinding() will be fired by extend().

So extend() will have different affect on rebinding() under bind() and instance(), is it reasonable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions