Understanding Callbacks and Anonymous Functions in PHP Laravel

Sandeeppant
2 min readFeb 22, 2025

--

In Laravel (and PHP in general), callbacks and anonymous functions are concepts that are very much related to eachother and often used interchangeably. They are crucial tools for handling operations like routing, event handling, middleware, and more.

What Are Anonymous Functions in PHP?

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. This feature allows you to define functions inline, without having to declare them globally. You can assign them to variables, pass them as arguments, or return them from other functions.

Anonymous functions are implemented using the Closure class.

<?php
$greet = function($name) {
printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>

But in Laravel they are you can see uses of anonymous function more frequently for routing, validation, in middleware , closures etc. For examples:

// anonymous function in closures;

User::query()->join('posts', function($q){
$q->where('is_active', true);
});

// like in routes
Route::get('/', function(){
return view('welcome');
});

// in collection
$users = collect([1, 2, 3, 4]);
$squared = $users->map(function ($item) {
return $item * $item;
});

Callback Functions in PHP:

A callback is a function that is passed as an argument to another function and is executed at a later time. Callbacks are useful when you want to customize behavior without changing the logic of the original function.

A callback can be an anonymous function or a named function. Laravel often uses callbacks to execute functions after certain events or conditions are met.

Example of callback function with Anonymous Function:

$users = [ 
['name' => 'abc', 'age' => 25],
['name' => 'xyz', 'age' => 30]
];

// Using a callback to filter users over 28 years old
$over28 = array_filter($users, function($user) {
return $user['age'] > 28;
});

print_r($over28); // Output: Array with 'xyz' as the only element

Key Differences:

  • Anonymous Function: It is simply a function with no name, which can be used wherever a function is expected.
  • Callback: It is any function (anonymous or named) that is passed as an argument to another function, and is called later when required.

In summary, anonymous functions are a subset of callbacks. In Laravel, you will often see anonymous functions used as callbacks, especially in routes, event handling, and other parts of the framework’s infrastructure.

Thank you for Reading …..

Before you go don’t forget to clap, share and follow me 😇😇😇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response