Skip to content

Instantly share code, notes, and snippets.

@pmjones
Created May 27, 2016 15:35
Show Gist options
  • Save pmjones/1e50bcefa227792f1297e17a5523282a to your computer and use it in GitHub Desktop.
Save pmjones/1e50bcefa227792f1297e17a5523282a to your computer and use it in GitHub Desktop.
<?php
// add to the end of FactoryTest
public function testSetterCallable()
{
$this->factory->setter['Aura\Di\FakeChildClass']['setFake'] = function () {
return 'OOPS';
};
$object = $this->factory->newInstance('Aura\Di\FakeChildClass');
$actual = $object->getFake();
$this->assertInstanceOf('Closure', $actual);
}
@jeremykendall
Copy link

What I'm trying to do looks more like this:

$this->factory->setter['Aura\Di\FakeChildClass']['setFake'] = function () {
    return function (ClassName $class) use ($otherThing) { // Do stuff, return something };
};

That results in a NOTICE about the curried variable, $otherThing, being undefined.

@pmjones
Copy link
Author

pmjones commented May 27, 2016

That results in a NOTICE about the curried variable, $otherThing, being undefined.

/me nods

That's because $otherThing is out-of-scope; you would need to use() it in the outer closure, then you can use() it in the inner closure.

What's the end-result you're trying to achieve by passing the closure?

@jeremykendall
Copy link

Out of scope. Geez. I'm really tired.

The closure is intended to be invoked during authentication. I get to customize the authentication validation while using a third party library to take care of all the DB access and such.

@pmjones
Copy link
Author

pmjones commented May 27, 2016

So, you should be able to pass that inner closure by itself to the setter, and the DI system will use that closure itself as the value for the setter. Then you can retain it as a property (or whatever) and invoke it inside the class later. Does that help?

@jeremykendall
Copy link

Totally. I'll test after lunch. Probably just an overtired scope issue :-)

@pmjones
Copy link
Author

pmjones commented May 27, 2016

Get some rest man. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment