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.