Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PetroGromovo's avatar

How defining event hook to use constant of other service ?

I try on laravel / livewire site to define event hook to use constant of other service :

    #[On('echo-private:' . (new ChatMessagesService)->EMPLOYEES_CHAT . ',EmployeeChatMessageEvent')]

and define constant :

class ChatMessagesService
{
    const EMPLOYEES_CHAT = 'EMPLOYEES_CHAT';
    ...

I got error :

   Fetching properties on non-enums in constant expressions is not allowed

If I restore string constant, it works ok :

    #[On('echo-private:EMPLOYEES_CHAT,EmployeeChatMessageEvent')]

Is there is a way in construction define to use constant of the class ?

    "php": "^8.2",
    "laravel/framework": "^11.27.2",
    "livewire/livewire": "^3.5.10",

Thanks in advance!

0 likes
1 reply
spoyntersmith's avatar

Hey @petrogromovo 👋

Instead of:

    #[On('echo-private:' . (new ChatMessagesService)->EMPLOYEES_CHAT . ',EmployeeChatMessageEvent')]

Try:

    #[On('echo-private:' . ChatMessagesService::EMPLOYEES_CHAT . ',EmployeeChatMessageEvent')]

For another example:

class Post
{
    const STATUS_PUBLISHED = 'published';
}

echo Post::STATUS_PUBLISHED; // "published"
1 like

Please or to participate in this conversation.