sitilge
sitilge

Reputation: 3737

How to keep object object properties in PHP?

I recently ran across a problem: object properties are recreated every time a new object is created. The flow:

  1. a new instance of Bootstrap class is created under index.php
  2. a new instance of the X class is created under Bootstrap

The point is to keep all the properties of class X by not creating a new instance of X if it has been already created once. What would be the best way to achieve this? Or maybe it is the flow itself that should be changed?

Thanks

Upvotes: 2

Views: 64

Answers (1)

Mostafa Talebi
Mostafa Talebi

Reputation: 9173

$x = new Bootstrap();

and somewhere in the bootstrap class:

private function instance_maker ()
{
   if($_SESSION['made_instance']=="")
   {
      //make instance
      $m = new Subclass();

      // set session to prevent further instanciation
      $_SESSION['made_instance'] = true;
   }
}

Upvotes: 1

Related Questions