Skip to content

Make routines accept instances that are callable#87

Merged
alganet merged 3 commits intodevelopfrom
issue-85
Jun 9, 2013
Merged

Make routines accept instances that are callable#87
alganet merged 3 commits intodevelopfrom
issue-85

Conversation

@augustohp
Copy link
Copy Markdown
Member

As noted on issue #85 I added the possibility of passing callable instances to routines. Let me hear what you people think, please.

Augusto Pascutti added 3 commits February 8, 2013 00:22
In other words, instances of classes that implement the __invoke()
method.
To accept class names instead of only instances we need to change
the validation of callable objects in Routine\AbstractRoutine,
something that would be nice to have. :)

Also created a *src* directory inside tests to put classes related
to tests only (stubs). Let me know if this bothers you a lot, I
still think that this is better than declaring multiple namespaces
on files.
- Creates an execute() method in AbstractSyncedRoutine;
- Creates test for Respect\Rest\Routines\AbstractRoutine:
    - Add provider for valid constructor arguments
    - Add assert for getCallback() method on AbstractRoutine.
@nickl-
Copy link
Copy Markdown
Member

nickl- commented Feb 17, 2013

This is awesome definitely a useful addition and arguably incomplete without it.

@nickl-
Copy link
Copy Markdown
Member

nickl- commented Feb 18, 2013

Some other peculiarities with Reflection on callbacks

php > $func = function () { return "I'm funky!"; }
php > is_callable($func)
true
php > $func()
"I'm funky!"
php > return var_export($func);
Closure::__set_state(array(
))

php > class INVOK { public function __invoke() { return "Invoke me bebe!"; }}
php > $invok = new INVOK;
php > is_callable($invok)
true
php > $invok()
"Invoke me bebe!"
php > var_export($invok)
INVOK::__set_state(array(
))

php > method_exists($func, '__invoke')
true

php > $rmeth = new ReflectionMethod($func, '__invoke');
php > $rmeth->invoke($func);
"I'm funky!"
php > $rmeth->invoke($func);
null
php > $rmeth->invoke($func);
null

php > $rmeth = new ReflectionMethod(new INVOK, '__invoke');
php > $rmeth->invoke(new INVOK);
"Invoke me bebe!"
php > $rmeth->invoke(new INVOK);
"Invoke me bebe!"
php > $rmeth->invoke(new INVOK);
"Invoke me bebe!"

php > $rfunc = new ReflectionFunction($func);
php > $func->invoke($func);
"I'm funky!"
php > $rfunc->invoke($func);
"I'm funky!"
php > $rfunc->invoke($func);
"I'm funky!"

php > $rfunc = new ReflectionFunction(new INVOK);
php > $rfunc->invoke(new INVOK);
PHP Fatal error:  ReflectionFunction::invoke(): Internal error: 
      Failed to retrieve the reflection object in ... 
null

php > $rfunc = new ReflectionFunction($invok);
Error Type: E_WARNING 
      Message: ReflectionFunction::__construct() expects parameter 1 to be string, object given
ReflectionFunction

php > $rfunc->invoke($invok);
PHP Fatal error:  ReflectionFunction::invoke(): 
      Internal error: Failed to retrieve the reflection object in ...
null

php > new Closure
Error Type: E_RECOVERABLE_ERROR 
      Message: Instantiation of 'Closure' is not allowed
null

php > $rfunc = new ReflectionFunction(new Closure);
Error Type: E_RECOVERABLE_ERROR 
      Message: Instantiation of 'Closure' is not allowed
null

php > $rfunc = new ReflectionFunction(Closure);
Error Type: E_NOTICE 
      Message: Use of undefined constant Closure - assumed 'Closure'
null

php > $rfunc = new ReflectionFunction('Closure');
Uncaught exception: Type: ReflectionException
      Message: Function Closure() does not exist
null

php > $rmeth = new ReflectionMethod(new Closure, '__invoke');
Error Type: E_RECOVERABLE_ERROR 
      Message: Instantiation of 'Closure' is not allowed
ReflectionMethod

php > $rmeth->invoke($func);
"I'm funky!"

@augustohp
Copy link
Copy Markdown
Member Author

I will make the changes to make it simpler. I went from many iterations and just want to make availiable so the guy with problems could test it.
I will fix up the code and merge it back to master and develop!

@nickl-
Copy link
Copy Markdown
Member

nickl- commented Feb 22, 2013

Overall lots of good work, well done! =) 🐼

@nickl-
Copy link
Copy Markdown
Member

nickl- commented May 1, 2013

@augustohp need a hand with this? Or has it already been committed elsewhere?

alganet added a commit that referenced this pull request Jun 9, 2013
Make routines accept instances that are callable
@alganet alganet merged commit e02c0e6 into develop Jun 9, 2013
@henriquemoody henriquemoody deleted the issue-85 branch January 12, 2015 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants