Can callback methods in PHP session_set_save_handler be private?

I am writing a special session handler in PHP and am trying to make the methods defined in session_set_save_handler private.

session_set_save_handler(
    array('Session','open'),
    array('Session','close'),
    array('Session','read'),
    array('Session','write'),
    array('Session','destroy'),
    array('Session','gc')
);

For example, I can configure an open function as confidential without any errors, but when I make the recording method private, it barks at me.

Fatal error: calling private method Session :: write () from context '' in Unknown at line 0

I'm just wondering if this was a mistake, or is there a way around this. The ban on the fact that I can, of course, just make it publicly available, but I would prefer. Last year, a publication appeared on php.net that talked about a similar thing, but I just want to know if anyone has any ideas. Does it really matter? I use PHP 5.2.0 in my development window, but it can certainly be updated.

+3
source share
2 answers

They must be public. Your class is created and called just like in your own code.

So, if you cannot figure out how to publicly call a private method for ANY class, then no = P

+4
source

Pass the instance of the object as the first parameter of your callback array.

$session = new Session();
session_set_save_handler(
    array($session,'open'),
    array($session,'close'),
    array($session,'read'),
    array($session,'write'),
    array($session,'destroy'),
    array($session,'gc')
);
0
source

Source: https://habr.com/ru/post/1698897/


All Articles