Closure is a function or a reference for a function together with a table that includes references for each one of the non local variables that exist within the outer scope of the closure. Unlike a plain function pointer the closure can use those non local variables that belong to its outer scope even when invoked out of it.

<?php
class Something
{
	private $number;
	private $person;
	public function __construct($num)
	{
		$this->number = $num;
	}
	public function getFunction()
	{
		return function()
		{
			for($i=1;$i<=$this->number;$i++)
			{
				echo "<br>gaga";
			}
		};
	}
}

$obA = new Something(7);
$obB = new Something(3);
$temp = $obB->getFunction();
$temp();

?>

The following video clip shows the execution of this code sample, overviews it and explains it.

Leave a Reply

Your email address will not be published. Required fields are marked *