undefined class constant (function)
| From: | Jeffry Killen | Date: | Tue, 20 Mar 2018 05:41:45 +0000 |
| Subject: | undefined class constant (function) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-326393@lists.php.net to get a copy of this message | ||
Hello;
I am trying to call either of two defined functions in a class def that
are assigned to associative array items.
$_proc = [];
$_proc['sync'] = self::listSync;
$_proc['async'] = self::listAsync;
return $_proc[$_type]($_fileLst);
"Fatal error: undefined class constant listSync...."
private function listSync(<arg>)
{
// code, return result;
}
private function listAsync(<arg>)
{
// code, return result;
}
I thought that if I did it like:
$_proc['sync'] = self::listSync()
self::listSync would be run and $_proc['sync'] would contain
what is returned.
So, trying to avoid a round of if/else if or switch block, how would I
do this?
I thought that this might work like javascript (I know javascript is a different language
but is mostly translatable to php). In js a function can be assigned to a variable and
called via the variable at a later time.
While composing this it might not be a bad idea that $_proc[<label>] would
contain the result of the function. So I probably answered my own question
for the task at hand. But allowing the $_proc items to hold function result
means that both function would have to be run so a statement like
return $_proc[$_type];
would work.
For future reference, how would I assign a function to a variable for delayed
execution in this situation?
Thanks for time and attention
JK.