How do I call $greet
inside this class? I am using PHP 5.5.4.
<?PHP
class Model
{
public $greet = function($name)
{
printf("Hello %s
", $name);
};
}
$test = new Model();
$test->greet('World');
$test->greet('PHP');
?>
Parse error: syntax error, unexpected '$greet' (T_VARIABLE), expecting function (T_FUNCTION)
Also tried this,
$test = new Model();
call_user_func($test->greet('World'));
call_user_func($test->greet('PHP'))
The anonymous function works fine outside the class (straight from the manual).
<?php
$greet = function($name)
{
printf("Hello %s
", $name);
};
$greet('World');
$greet('PHP');
?>
EDIT: I took out the dollar signs, in my call (I caught it just as an answer started to roll in. It did not help,
call_user_func($test->greet('World'));
call_user_func($test->greet('PHP'));
EDIT:
class Model
{
public $greet;
function __construct()
{
$this->greet = function($name)
{
printf("Hello %s
", $name);
};
}
}
$test = new Model();
$test->greet('johnny');
Now I get,
Fatal error: Call to undefined method Model::greet()