Say I have a php file called a.php:
//a.php
<?php require_once('b.php'); ?>
In b.php I have:
//b.php
<?php
if (!$obj) $obj = new myclass();
?>
Then what’s the lifespan of $obj?
If I refresh a.php several times, will I get multiple instances of myclass?
Also if I want to use $obj reference in other pages, how can I achieve that?
The object lives at MOST until the script ends, and in general objects do NOT persist between page loads. You can reference $obj from other scripts which are included from a.php, but only if they are included on the same request… Other requests may also include b.php, but it will indeed be a new instance of myclass.
In order to refer to the same entity (i.e. the same conceptual object), you need a persistence mechanism, and a way to initialize any given instance of myclass with the persisted data (which is then usually passed via URL parameters).