andygrant Posted November 28, 2008 Share Posted November 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134648-access-all-variable-instances-of-specific-classes/ Share on other sites More sharing options...
rhodesa Posted November 28, 2008 Share Posted November 28, 2008 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(); Link to comment https://forums.phpfreaks.com/topic/134648-access-all-variable-instances-of-specific-classes/#findComment-701065 Share on other sites More sharing options...
andygrant Posted November 28, 2008 Author Share Posted November 28, 2008 That should do the trick, thanks. Link to comment https://forums.phpfreaks.com/topic/134648-access-all-variable-instances-of-specific-classes/#findComment-701090 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.