Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active December 8, 2016 14:42
Show Gist options
  • Save mnapoli/f8afef0f556010c144e7 to your computer and use it in GitHub Desktop.
Save mnapoli/f8afef0f556010c144e7 to your computer and use it in GitHub Desktop.
Extended callables

PHP callables

Those are callables recognized by PHP natively. They can be invoked simply: $callable(...).

  • Closures: function () { ... }

  • Static method call: ['A', 'staticMethod'] or also Foo::staticMethod

    class A {
        public static function staticMethod() { ... }
    }
  • Method call: [new B, 'method']

    class B {
        public function method() { ... }
    }
  • Invokable class: new C

    class C {
        public function __invoke() { ... }
    }
  • Function call: 'hello'

    function hello() { ... }

Extended callables

Extended callables are a made-up thing not recognized by PHP. They can be called using Invoker and are used for example in the Silly framework.

  • Method call: ['B', 'method']

    class B {
        public function method() { ... }
    }

    Warning: the method must not be static, else it will be recognized as a normal PHP callable representing a static method call.

  • Invokable class: 'C'

    class C {
        public function __invoke() { ... }
    }

    Warning: C must not be a function, else it will be recognized as a normal PHP callable representing a function call.

The Invoker will instantiate the object instances using any Container. The benefits are:

  • lazy creation of objects
  • objects are created by containers, which means the container can inject dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment