Recently, in one of my projects I needed to pass a local function to "stack" it.
What is a local function?
In php a local function is a function within a function
What I needed?
Well, basically I needed is to stack function calls in a list, so I could call it later.
In PHP you have the callable type, and you can call any function that is globally accessible or a method of an object which is globally accessible, knowing the function's name.
The solution
I used an interface to do something like this:
What do you thing?
Have I done an complicated workaround just to skip doing a function globally available and protected by a namespace?
Top comments (2)
I think maybe your solution is apply Strategy Pattern or State Pattern, you need to analyze your situation and make the choice.
BTW
function pushFunction( &$afunction ) {
isn't neccesary, in PHP objects are assigned to variables with a identifier point, the same point always unless you clone the object.Good points!
The goal in the real project is different, and i'm not sure at all that Startegy pattern or State Patter will do what I was looking for.
But you've shown me an interesting new site! designpatternsphp.readthedocs.io
And true, & is not necessary here.
Thx!