DEV Community

Eduard Llach
Eduard Llach

Posted on

5 2

Stack local functions in PHP

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

<?php
function main() {
//Some code can go here
function local() {
//This function does something
}
//Some code can go here which can call local();
}

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:

<?php
/**
* Example showing how to stack functions and call them later.
* Just as Javascript addEventListener would do: Passing functions like callable objects
* Solution if functions are "local" and not globally available
**/
interface mycallable {
public function call($param);
}
var $stack = [];
/**
* This function will push the function into the array, with some basic validation
**/
function pushFunction( &$afunction ) {
global $stack;
if( $afunction instanceof mycallable ) {
$stack[] = $afunction;
} else {
throw new Exception('trying to push something that is not a mycallable');
}
}
/**
* This function will call all stacked functions
**/
function callStackedFunctions() {
global $stack;
foreach( $stack as $afunction ) {
$afunction->call(null);
}
}
/**
* This functions does real things
**/
function doThings() {
//Some code here
if( $condition ) {
class aFunction implements mycallable {
public function call($param) {
//do some stuff
}
}
$variable = new aFunction();
} else {
class bFunction implements mycallable {
public function call($param) {
//do other stuff
}
}
$variable = new bFunction();
}
pushFunction($variable);
}

What do you thing?
Have I done an complicated workaround just to skip doing a function globally available and protected by a namespace?

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (2)

Collapse
 
kip13 profile image
kip

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.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
php.net/manual/en/language.oop5.re...

Collapse
 
eduardllach profile image
Eduard Llach

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!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs