
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Closure class
Introduction
Anonymous functions (also called lambda) return object of Closure class. This class has some additional methods that provide further control over anonymous functions.
Syntax
Closure { /* Methods */ private __construct ( void ) public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure public call ( object $newthis [, mixed $... ] ) : mixed public static fromCallable ( callable $callable ) : Closure }
Methods
private Closure::__construct ( void ) — This method exists only to disallow instantiation of the Closure class. Objects of this class are created by anonymous function.
public static Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) − Closure — Duplicates a closure with a specific bound object and class scope. This method is a static version of Closure::bindTo().
public Closure::bindTo ( object $newthis [, mixed $newscope = "static" ] ) − Closure — Duplicates the closure with a new bound object and class scope. Creates and returns a new anonymous function with the same body and bound variables, but with a different object and a new class scope.
public Closure::call ( object $newthis [, mixed $... ] ) − mixed — Temporarily binds the closure to newthis, and calls it with any given parameters.
Closure Example
<?php class A { public $nm; function __construct($x){ $this->nm=$x; } } // Using call method $hello = function() { return "Hello " . $this->nm; }; echo $hello->call(new A("Amar")). "
";; // using bind method $sayhello = $hello->bindTo(new A("Amar"),'A'); echo $sayhello(); ?>
Output
Above program shows following output
Hello Amar Hello Amar
- Related Articles
- PHP Class Abstraction
- PHP Class Constants
- PHP Class properties
- PHP Generator class
- PHP WeakReference class
- Explain abstract class in PHP.
- Transitive closure of a Graph
- What is the ;closure property?
- Explain Closure property under addition.
- PHP Basics of Class and Object
- Explain closure of attributes in DBMS
- What is JavaScript closure? Explain with examples.
- What is an epsilon closure in TOC?
- What is the closure property of integers?
- Explain Closure property of multiplication in integers.
