Objects and Memory Permanence in PHP

Asked

Viewed 412 times

0

It is possible to keep objects in memory during a session in PHP or you need to use the "Session" variable to store the previously instantiated objects?

The goal is to create a web system similar to a conventional OO application, only in case the server waits for the user’s response to modify the state of the objects. As far as I understand it, every new request will need to recreate the objects instead of changing the state of the ones that already existed. Is there any way to keep them in memory?

  • 1

    What you mean by session in this context?

  • Thank you for your interest in answering the question, Anderson! Session, start and end set of a number of services. My idea is to implement the services through Objects that would be available while the session is logged in by the user, but from what I understood from the readings I did before I asked this question is that there is no way to keep objects in memory while waiting for user interaction.

  • Why would you want to do that?

  • Good afternoon, bfavaretto! Thanks for the comment. The goal is to create a web system similar to a conventional OO application, only in case the server waits for the user’s response to modify the state of the objects. As far as I understand, each new request will be necessary to recreate the objects instead of changing the state of the ones that already existed, which to me does not seem something practical or interesting... Precisely why the question.

  • 4

    @user3081078 which is one of the reasons I abhor the use of OOP in PHP. Very cool that you have had this concern, is around, I think you are starting to realize something that unfortunately many people who "think you understand" PHP did not stop to think. In most cases, while the OOP code is reassembling the object that came from the previous script, the procedural code has already done what it had to do and finished a long time ago. They took a concept that came from languages that maintain the state, and applied it to a script engine, which ironically... does not maintain state, which is basic to justify an object.

  • Thank you for the reply @Bacco! I am happy and sad at the same time to know that I will need to work with serialization, since the language of the system has already been established. I appreciate the contribution!

  • 3

    Or you can use PHP as it was originally designed, tending to the procedural and leaving OOP aside :D - Probably the code will get, among other things, more readable and absurdly thinner - remembering that as the language allows, you can use the two paradigms as convenience.

  • 1

    Not necessarily with serialization. You can reinstall everything with every request. Not necessarily serializing/deserialize will give you performance gains. Or you can follow @Bacco’s suggestion :)

  • 3

    These @Bacco comments should be an answer, and those canonical ones should stand out

  • 1

    You may not want to "radicalize" to the point of @Bacco’s suggestion to abandon OOP, but it’s no use wanting to make the application work in the same way as in environments that maintain status. In my opinion, serializing the state is the worst way out, unless it solves some performance issue that the application is facing.

  • Guys, thank you for your contributions. I think I’m going to have to create a mechanism for serializing the state of the objects in which this is necessary. Unfortunately I need to use PHP and, at the moment, recreate all the mechanisms I had designed will take time that I do not have for the project! And I agree with Maniero, @Bacco’s comments certainly served as an answer to my question.

  • One last addendum, is there any backend web language other than Java Server Pages that maintains the state of the created objects? I got this flea behind my ear also with Python (Django) and Javascript (Node.js).

Show 7 more comments

2 answers

0

Good afternoon, using the serialize() and unserialize() functions it is possible to store objects in $_Sessions. For example:

<?php
class UserTeste {
    private $nome = 'teste';
    public function getNome() {return $this->nome;}
}
$user = new UserTeste();
echo $user->getNome(); // teste

$_SESSION['user'] = serialize($user);

echo $_SESSION['user']; // O:9:"UserTeste":1:{s:15:"UserTestenome";s:5:"teste";}

$outroUser = unserialize($_SESSION['user']);

echo $outroUser->getNome(); // teste

?>

Use serialize() encode your objects to be stored, just be sure to reverse the serialization with unserialize() otherwise the same will happen in the second echo.

  • Thanks for the reply Dalabona! But is there any way to keep the object in memory to use object orientation without using serialization? This is my question. and I believe the answer is no.

  • When I needed to use it at other times, I had to use serialize. I don’t know any other method.

  • Dalabona perfect, thanks for the answer!

0


As clarified in the comments of the question, the only way to maintain the state of the objects with each PHP request is to save them as a session variable.

To continue the development in Object Orientation I developed an auxiliary class called "Object Manager", whose function is to keep the state of the objects in session in a simple way.

If you’re curious, it’s just access here the project.

Thank you to everyone who helped clear my doubts!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.