Welcome! Log In Create A New Profile

Advanced

[PHP] undefined class constant (function)

Posted by Jeffry Killen 
Jeffry Killen
[PHP] undefined class constant (function)
March 20, 2018 06:50AM
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: undefined class constant (function)
March 20, 2018 11:20PM
On 20.03.2018 at 06:41, Jeffry Killen:

> 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.

That is possible in PHP as well, but you have to pass a valid callable,
see http://www.php.net/manual/en/language.types.callable.php for details.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] Re: undefined class constant (function)
March 21, 2018 12:20AM
Thank you for the reply:

While I was waiting I came up with the following as a solution:

$_proc = [];
$_proc['sync'] = function($_a){return self::listSync($_a);};
$_proc['async'] = function($_a){return self::listAsync($_a);};
$_out = $_proc[$_type]($_fileLst);
return $_out;

which I have working.

But I will take a look at your reference.

I was thinking of call() or apply() but I really don't understand those functions.

I look at documentation and think I understand them, but when I see them in use
I mostly don't.

JK
> On Mar 20, 2018, at 3:18 PM, Christoph M. Becker <[email protected]> wrote:
>
> On 20.03.2018 at 06:41, Jeffry Killen:
>
>> 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.
>
> That is possible in PHP as well, but you have to pass a valid callable,
> see http://www.php.net/manual/en/language.types.callable.php for details.
>
> --
> Christoph M. Becker
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login