Jump to content

Access all variable instances of specific classes


andygrant

Recommended Posts

Hi

 

I was wondering if anyone knew if it was possible to access all variable that are instances of a specific type of object. So if I had a user class and created a few of these through the code could I get access to all later without keeping track in some way as they are created.

 

Thanks in advance

you can use a static variable in the class to keep track:

 

class user {

  private static $users = array();

  function __construct ( ) {

    self::$users[] = $this;
  }

  static function getUsers ( ) {
    return self::$users;
  }
}

$user1 = new user();
$user2 = new user();
$user3 = new user();

$users = user::getUsers();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.