Superglobal object in php

2

I am clear that to store some data in a persistent global way so that it is accessible from any file in php, the session variables must be used.

But apparently it can not store objects, only arrays and little else.

Is there a way to simulate this?

The object that I have has variables that store open sockets and I think that it can not be stored well.

    
asked by Takyo 03.02.2017 в 12:50
source

3 answers

1

You are really asking several questions simultaneously, some can be done and others can not.

Can an object be saved in a super global variable? Yes

function pruebas() {
  var_dump($GLOBALS['prueba']);
}

class prueba {
 private $a = 5;
 public $b = 8;
}

$GLOBALS['prueba'] = new prueba();
pruebas();

Can an object be saved in a session variable? Yes

session_start();

class prueba {
 private $a = 5;
 public $b = 8;
}

if (!isset($_SESSION['prueba'])) {
  echo "<p>Creando</p>\n";
  $_SESSION['prueba'] = new prueba();
} else {
  echo "<p>Ya estaba creado</p>\n";
}
var_dump($_SESSION['prueba']);

Can a resource be saved in a session variable? NO

You can not save resources that can be used between different executions of PHP scripts because they are closed at the end of the execution, the file descriptors (among which are the sockets) are also released at the end of the execution, so that can not be reused.

Connections to persistent databases or databases

There is a lot of confusion on this issue: there are solutions to reuse previously established connections (such as TCP or UNIX sockets or persistent database connections ), but you are not guaranteed in any way that it will be exactly the same connection that is used the next time you connect.

If you try to open a socket or database connection that is being used by another script, a new connection will be created to satisfy the need for your script.

In the PHP documentation about connections to persistent databases it can be read:

  

Those who are not fully familiar with the way in which   they work and distribute the load the web servers could confuse   what are the persistent connections for? In particular, with them   you can not open "user sessions" on the same link, you can not   can build an efficient transaction and do not do many other   things. In fact, to be extremely precise, the connections   persistent do not provide any other functionality that was not   possible to do with your non-persistent sisters.

    
answered by 03.02.2017 / 14:44
source
0

Responding to your question, to store objects in memory you could use something like memcached (my translation):

  

Distributed system of free, high-performance, free-code data and object caching, generic in nature, but intended to be used to accelerate dynamic web applications by easing database load.

     

Memcached is a storage of key-values in memory for small groups of arbitrary data (chains, objects) for results of calls to databases, calls to APIs or page rendering.

     

Memcached is simple but powerful. Its simple design promotes rapid distribution, ease of development, and solves many problems encountered by large data caches. Its API is available for many different languages.

But from what you put in the comments, the problem you have seems to be different from what you ask. You have a variable of type socket and you want it to be persistent. That's something you could perhaps get by opening the socket with pfsockopen (emphasis mine):

  

pfsockopen - Open persistent Internet or Unix domain socket connection

     

This function behaves exactly like fsockopen () with the difference that the connection does not close after the script ends . This is the persistent version of fsockopen ().

Although it must be a UNIX domain socket, I do not know if it's your case.

    
answered by 03.02.2017 в 14:37
-1

I think you should use a database or a file (preferably the first one).

You create a RAM table and you do it there, the session is not the right place to store a large amount of data.

It is the best way to make it persistent between requests. You can use a session number to identify the user in question within these tables.

    
answered by 03.02.2017 в 13:49