Open In App

PHP 7 Closure call( ) Method

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

PHP 7 Closure::call() method can bind an object scope, along with calling the closure temporarily. It takes the argument as an object and a variable number of parameters to be bound.

Syntax:

public Closure::call(object $newThis, mixed ...$args): mixed

Parameters:  It takes two parameters, which are described below:

  • $newThis: This parameter represents an object that is used to bind the closure while making the call.
  • $args: This parameter specifies the number of arguments, i.e. more than zero, that will be passed as parameters to the closure.

Return Value: The return value for the Closure will be returned by this function.

Example 1: This code snippet declares a class “Person” with a constructor that takes two arguments  $name and $age. It then creates an instance of the “Person” class and assigns it to the variable $person. Finally, it prints the person’s name and age using the getName() and getAge() methods of the $person object.

PHP




<?php
class Person
{
    private $name;
    private $age;
  
    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
  
    public function getName()
    {
        return $this->name;
    }
  
    public function getAge()
    {
        return $this->age;
    }
}
  
$person = new Person("Geek", 23);
echo "Name: " . $person->getName() . "\n";
echo "Age: " . $person->getAge() . "\n";
?>


Output:

Name: Geek
Age: 23

Example 2:The code defines a “Geekcalculator” class with an add() method that creates a closure to call the add() method of the current object. It then uses the Closure::call() method to call the closure with the current object and the given arguments. The result is returned and printed on the screen.

PHP




<?php
class Geekcalculator
{
    public function add($a, $b)
    {
        // Create a closure that calls the `addNumbers` 
        // method of the current object
        $sum = function ($c, $d) {
            return $this->addNumbers($c, $d);
        };
  
        // Call the closure with $a and $b as arguments
        return $sum($a, $b);
    }
  
    // `addNumbers` method simply adds two numbers
    public function addNumbers($x, $y)
    {
        return $x + $y;
    }
}
  
// Create an instance of the `Geekcalculator` class
$calculator = new Geekcalculator();
  
// Call the `add` method with arguments 69 and 31
// the `add` method creates a closure that calls
// the `addNumbers` method of the `Geekcalculator`
// object, and then calls that closure with
// the provided arguments
$result = $calculator->add(69, 31);
  
// Output the result
echo $result;
?>


Output:

100

Reference: https://www.php.net/manual/en/closure.call.php



Similar Reads

Program to swap two integer parameters using call by value & call by address in PHP ?
Call by value: In call by value, we will send the value from the calling function to the called function. The values of the calling function arguments are directly copied to the corresponding arguments of the called function. If any modification is done on the arguments of the called function, it will not be effected on the arguments of the calling
3 min read
Call by Value Vs Call by Reference in JavaScript
Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a variable named “a”. Now, we store a primitive value(boo
3 min read
What is the practical use for a closure in JavaScript?
In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures: Example: In this example, a variable mult is defined that is local to the function multFn and is only accessible inside this function
2 min read
How to use a closure to create a private counter in JavaScript ?
A closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. Example: Here the inner forms closure with outer. The str variable can be called a private member whose value we can
4 min read
Describe closure concept in JavaScript
In this article, you will learn about the concept of closure in Javascript &amp; how to apply it on various functions. The closure in javascript is a way to combine all the functions &amp; binds them in bundles with the lexical environment. We will deep dive into the closure concept as we grow in this article. Prerequisite: Before we proceed to our
9 min read
How closure works in JavaScript ?
In this article, we will discuss about the closures working JavaScript. Let us first understand what exactly closures are and basic details which are associated with closures in JavaScript. A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environment). In JavaScript, closures are created every
2 min read
Relation of Garbage Collector and Closure in JavaScript
Garbage Collector: A program in the browser on the JavaScript engine that kind of freezes up the unutilized memory. In the programming languages like C /C++, we generally decide how we can take memory how we have the access to the memory or how we can allocate or deallocate the memory, it's basically up to the developers how they will use it. But i
2 min read
Closure in JavaScript
Closures are a powerful and fundamental concept in JavaScript. They allow inner functions to access variables from their outer scope, even after the outer function has completed execution. In simpler terms, a closure “closes over” its lexical scope, preserving the variables and scope of the outer function. In this article, we’ll dive deep into clos
4 min read
How to call PHP function from string stored in a Variable
Given the names of some user-defined functions stored as strings in variables. The task is to call the functions using the names stored in the variables. Example: &lt;?php // Function without argument function func() { echo &quot;geek&quot;; } // Function with argument function fun($msg) { echo $msg; } // Call func and fun using $var and $var1 $var
2 min read
Print PHP Call Stack
Given a PHP code and task is to print call stack for this PHP code. In the given PHP code, a child_func() function calls parent_func() function which further calls grandparent_func() function resulting in a call stack. Approach 1: Print the call stack using debug_print_backtrace() function. Example: &lt;?php // PHP program to print PHP call stack /
2 min read