A
A
Alexander Andreev2019-10-26 17:31:06
PHP
Alexander Andreev, 2019-10-26 17:31:06

Why doesn't Singleton save the parameters written to the property of the object?

Took an example of the Singltone class here , making a small parser. I want to save the path of the uploaded file, read it partially, so that the user can adjust the correspondence between the columns from the file and the directory properties at the second step of parsing (which, by the way, may also need to be stored in an instance of the Parser class). In general, if you do this:

$parser = App\Parser::getInstance();
$parser->setValue('name', 'parserPanel');

$parser2 = App\Parser::getInstance();
echo $parser2->getValue('name'); //тут получаю ранее установленный name, т.е. parserPanel

Then App\Parser::getInstance(); gives you what you need. If, however, getInstance() is called again in another file responsible for processing ajax requests (the App\Parser class is preliminarily connected in this file and an instance of the class is created), then I get a new object that does not contain any name. The question is why calling App\Parser::getInstance(); in two different files i get different object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
di, 2019-10-26
@maler1988

PHP is born, raises the whole environment, but dies after the request. When you make a new request, you get a new object...
A singleton in PHP only within one request (unless we are talking about asynchronous PHP of course)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question