add and call anonymous function in class php


Adding anonymous function and call the public variable in an anonymous function by using magic function called __invoke();

 

<?php
// Your code here!
class model
{
public $greet;
public $a =”hello”;
function __construct()
{
$this->greet = function(){
echo $this->a;
};
}
}
$model = new model();
$model->greet->__invoke();
?>

About

myself pramodh kumar yet another php developer from India and have worked on oops,procedural,yii framework,codeigniter,wordpress,joomla,api and more.

Tagged with: , , , ,
Posted in php

Leave a comment

Follow Tutorial on WordPress.com
categories
Calendar
June 2019
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
Follow me on Twitter
Blog Stats
  • 88,191 hits