Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

CodeWall:
Single Action Controller in Laravel
Aug 20, 2018 @ 14:10:38

On the CodeWall blog they've posted a tutorial showing how to create a single action controller in a Laravel-based application to isolate functionality that can be called directly with no method required.

There are some magical functions in PHP, and __invoke is one of them.

Through this __invoke method, we can create classes with just one function __invoke in them, and whenever their object is called it will directly call the __invoke method, it means you don’t have to manually say $obj->someFunction() .

The post starts by talking about the Single Responsibility Principle (SRP) and how having an isolated controller with just an __invoke method. It then walks you through the implementation and how to use a controller like this. In their example they show how to use it to create "post" records (like for a blog). The post also includes some of the drawbacks of using this kind of controller including the fact that you cannot use resource routing with it.

tagged: controller laravel singleaction invoke tutorial

Link: https://www.codewall.co.uk/single-action-controller-in-laravel/

BitExpert Blog:
Mocking callables in an Expressive app
May 01, 2017 @ 16:18:28

On the BitExpert blog Stephan Hochdörfer shows you how to mock callables in a Zend Expressive application based on a way he found during his own unit testing.

While working with Zend Expressive, a PSR-7 middleware microframework, I wanted to apply some unit testing with a nice coverage to my middlewares. Middlewares are called by the __invoke method if you provide them as an object and not as a closure. [...] Additionally, my middleware implementation does some stuff, but the middleware itself does not return a response, which is fine. Instead, my implementation calls the $next middleware in line.

He finishes the post with a quick example of how to mock out this $next call in his testing using the createPartialMock functionality in PHPUnit. He uses this method to create a mock that covers the __invoke method and returns a ResponseInterface instance of his choosing.

tagged: zendexpressive mocking phpunit unittest invoke callable testing tutorial

Link: https://blog.bitexpert.de/blog/mocking-callables-in-an-expressive-app/

Abdul Malik Ikhsan:
Start Using Middleware Approach with new zend-mvc
Mar 02, 2016 @ 18:54:37

Abdul Malik Ikhsan has a post to his site showing how you can integrate the concept of middleware in to the Zend MVC Component of the Zend Framework. Middleware has come into its own with the acceptance of of the PSR-7 standard and several frameworks adopting its structure for request/response handling.

zend-mvc 2.7.0 is coming, beside of the forward compatibility with V3 components, there is new middleware listener that allow us to do Dispatching PSR-7 middleware. The middleware can be an invokable class with __invoke() method.

He shows how, starting with a new project, to create the middleware class (in this case HomeAction) and the __invoke function. He then adds in the "home" route to the module configuration and points it at this HomeAction class. He registers it in the service manager as an InvokableFactory and it magically works. From there he refactors to allow the middleware to work with the ViewModel and change things like templates and variable values.

tagged: middlware zendframework zendmvc component psr7 tutorial invoke

Link: https://samsonasik.wordpress.com/2016/03/01/start-using-middleware-approach-with-new-zend-mvc/

Julien Pauli:
PHP closures
Jul 10, 2015 @ 15:54:29

Julien Pauli has posted a look at PHP's closures and how they're actually handled internal to the language.

Back in 2009, when PHP 5.3 got released, a new feature (among many others) were introduced : anonymous functions (also called lambdas or closures). The feature was very expected, as closures have proved their utility through several other languages, particularly javascript that web developers master. [...] Let's see together how Closures have been added to PHP, as usual by turning to the truth : the PHP source code.

He starts at the beginning (a good place to start) and talks about the work that needed to be done on the internals before closures could even be introduced. He walks through the changes made to object handling to make them "callable" and the addition of the "zend_closure" object type. He then gets to the part where "the magic happens" and shows how the userland closure is translated and executed. He ends the post with a look at two other topics: scoping with "$this" and the special handling that was needed for reflection and direct calls to "__invoke".

tagged: closure language functionality object callable scope reflection invoke

Link: http://jpauli.github.io/2015/07/08/php-closures.html

NetTuts.com:
Reflection in PHP
Apr 19, 2013 @ 15:24:28

On NetTuts.com today there's a new tutorial talking about a part of PHP that can be quite powerful but isn't used too often - reflection in PHP. Using Reflection you can get information about your actual code and its elements without having to try to parse it yourself.

Reflection is generally defined as a program’s ability to inspect itself and modify its logic at execution time. In less technical terms, reflection is asking an object to tell you about its properties and methods, and altering those members (even private ones). In this lesson, we’ll dig into how this is accomplished, and when it might prove useful.

They provide a little context around the idea of "reflection" in programming languages and then jump right in with a few sample classes. They set up their "Nettuts", "Manager" and "Editor" classes and show how to use the ReflectionClass functionality to get their structure. The examples show how to get the class' methods, their properties and calling these methods using things like invoke and call_user_func.

tagged: reflection tutorial introspection methods parameters invoke

Link: http://net.tutsplus.com/tutorials/php/reflection-in-php

Josh Adell's Blog:
Command Invoker Pattern with the Open/Closed Principle
Jan 16, 2012 @ 16:04:42

In a response to a recent post on DZone.com about the "Open/Closed Principle" Josh Adell has posted an example of a " flexible and extendable command invocation solution" implementing this SOLID idea.

Let's overcome some of these issues [with only being able to extend the invoker class and that the invoker needs to know how to create commands], and also make the code even more extensible. I'll use a simplified command invoker to demonstrate.

His code is included - the creation of a "Command" interface and two comments that implement it: "HelloCommand" and "PwdCommand", each with "register" and "execute" methods. His "Invoker" class then only needs to be told how to map these commands and the "register" is called as they're needed. You can find the full example code for this invocation example in this gist.

tagged: command designpattern invoke open closed principle solid tutorial

Link:

Ryan Gantt's Blog:
Anonymous recursion in PHP
Aug 11, 2011 @ 15:55:35

In a recent post to his blog Ryan Gantt looks at an interesting way to get around a limitation in PHP dealing with anonymous recursion and closures that throws a Fatal error when called.

Turns out that variables called as functions must be an instance of Closure, an instance of a class which implements __invoke(), or a string representing a named function in the global namespace. In the anonymous function body above, $fibonacci is none of these. It is an undeclared, free variable in the closure created by the anonymous function. At the time when it’s called, it hasn’t been bound—hence the Notice that you would have gotten if error reporting were set at a high enough threshold - and therefore can’t be called as anything, let alone as a function.

He tried using the "use" functionality PHP closures have to bring a variable/object/etc into the scope of the running function, but it still threw an error. As it turns out, the combination of "use"-ing the object and calling it by reference handles things correctly. He takes this method and applies it in two examples - one call in an array_map function and another in an array_reduce.

tagged: anonymous recursion reference invoke closure

Link:

Web Developer Juice:
PHP Magic Functions: Best Part of Object Oriented PHP - Part 2
May 19, 2011 @ 15:14:27

Web Developer Juice has posted the second part of their series looking at some of the "magic functions" that PHP has to offer - special functions that do automagic things in your scripts and classes. Part one can be found here.

In my previous post ( PHP Magic Functions ), I discussed about __construct, __destruct, __call and __callStatic. Lets explore a few more magic functions...

In this latest part of the series they look at three functions:

  • __set/__get
  • __invoke
tagged: magic function method oop get set invoke

Link:

DZone.com:
Reuse your closures with functors
Dec 29, 2010 @ 16:50:19

On DZone.com there's a new tutorial from Giorgio Sironi about reusing closures with the help of functors (a special kind of object instancing done in PHP with the help of __invoke).

I like PHP closures and their superset, anomyous functions, as they implement the Command pattern very well when used correctly. However I feel that sometimes they are: difficult to reuse and difficult to force contracts on. [...] What if we wanted instead, a closure which we can instance even more than one time (maybe with different variables), and that we could type hint?

He shows how to make this possible with a functor created using the __invoke magic method of PHP to handle the request to an object like a function. He includes some sample code to show it in action - a basic callback (SquareCallback) and how it compares to calling a normal closure. It also shows something a bit more technical, an "AdderCallback" class that can be defined as a type hint on a function.

tagged: closure functor invoke snippet example callback typehint

Link:

Jose da Silva's Blog:
Revisiting PHP 5.3 __invoke magic method
Nov 05, 2010 @ 17:42:04

In a new post to his blog Jose da Silva briefly looks at a feature that was introduced in the PHP 5.3.x series of the language - the __invoke magic method.

PHP version 5.3 introduced a new magic method designed __invoke, this method is called when a script tries to call an object as a function. [...] As php cannot accommodate pseudo-first-class functions, the __invoke method can be used to suppress this language limitation. On other hand you can use this for simpler things as pass a function around.

He includes a simple code example that shows a basic class being called via a variable name - $c('ford') - and the result of its __invoke method being called. He notes that the method, in his opinion, could make for less clean code.

tagged: invoke magic method opinion example

Link:


Trending Topics: