How to create a notification system that pushes messages to a user's specific account?
04:21 31 Aug 2023

I am trying do store a function in a file and extract to run when needed, but I cannot find a better way to do so.

This is what I've tried:

function store_callable(string $func)
{
    writefile($func);
}
eval(readfile());

My first concern is the type of the parameter $func. I'm expecting to receive only callable as argument, but I can't find a way to convert a PHP callable into string to save.

Secondly, although eval() is used here to run the saved function, I wondered if there's a better way of doing that, although seems there's no.

I don't think it's a kind of serialization but it seems they have similarities so I added the tag still.


EDIT:

My original idea was to create a notification system that pushes messages to a user's specific account. However, some notifications are specifically pushed only to one device or IP. Which means I need a filter function for that. I can actually complete this by using a new property like restricted-to but I wanted to make a more general field filter-function in the notification object which returns a boolean and lets the system decide whether to push the notification on this load.

EDIT2:

I CAN actually directly code them in the PHP file, but it will become messy, as different notification has different filter to decide whether they are pushed or not, I can give u an example:

new Notification("message");
class Notification
{
    public function push_or_not()
    {
        if($this->isabout== "login" && checkIP())
        {
            \\ sth
        }
        else if() 
        else if()
        //and so on
    }
}

This can actually run and avoid security risks. But this isn't general enough. Meaning that I need to edit the if-else here at first, and create a new type property here.

I'm just asking whether the initial more general filter function (cuz I can define it in EACH notification but here I can just add the type and relate it to the type) stored directly in notification data is better (and any improvements) or the latter one here is better.

It seems there's not much security risk here as the code here isn't related to user input.

php