[PHP] how to count objects instances
hi, What is best method(if it's possible) to count how many times an object is instanced in one time script call? I means that if i have a class named "Test", i what to know how many times she's called, how many copies exists etc. The idea is for monitoring in the way to optimizing the code. The method get_declared_classes() only shows only the classes included/required. Tanks -- This mail sent through Horde-Toaster (http://qmailtoaster.clikka.com/) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
[EMAIL PROTECTED] wrote: hi, What is best method(if it's possible) to count how many times an object is instanced in one time script call? I means that if i have a class named "Test", i what to know how many times she's called, how many copies exists etc. The idea is for monitoring in the way to optimizing the code. The method get_declared_classes() only shows only the classes included/required. There's no predefined variable or method for determining this, that I'm aware of, short of counting how many "new Test" lines you have. You could write a wrapper class for Test that kept count of the instances and returned a new object upon request... -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
On Mon, 2004-08-23 at 20:54, John Holmes wrote: > [EMAIL PROTECTED] wrote: > > hi, > > What is best method(if it's possible) to count how many times an object is > > instanced in one time script call? > > I means that if i have a class named "Test", i what to know how many times she's > > called, how many copies exists etc. The idea is for monitoring in the way to > > optimizing the code. > > > > The method get_declared_classes() only shows only the classes included/required. > > There's no predefined variable or method for determining this, that I'm > aware of, short of counting how many "new Test" lines you have. > > You could write a wrapper class for Test that kept count of the > instances and returned a new object upon request... In PHP5: http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
* Thus wrote Robert Cummings: > On Mon, 2004-08-23 at 20:54, John Holmes wrote: > > [EMAIL PROTECTED] wrote: > > > > You could write a wrapper class for Test that kept count of the > > instances and returned a new object upon request... > > In PHP5: > > > class Foo > { > static $instances = 0; > > function __construct() > { > Foo::$instances++; > } > > function __destruct() > { > Foo::$instances--; > } And: function __clone() { Foo::$instances++; } Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
On Mon, 2004-08-23 at 20:33, Curt Zirzow wrote: > * Thus wrote Robert Cummings: > > On Mon, 2004-08-23 at 20:54, John Holmes wrote: > > > [EMAIL PROTECTED] wrote: > > > > > > You could write a wrapper class for Test that kept count of the > > > instances and returned a new object upon request... > > > > In PHP5: > > > > > > > class Foo > > { > > static $instances = 0; > > > > function __construct() > > { > > Foo::$instances++; > > } > > > > function __destruct() > > { > > Foo::$instances--; > > } > > And: > function __clone() > { > Foo::$instances++; > } Perfect. I hadn't thought of that :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
Robert Cummings wrote: On Mon, 2004-08-23 at 20:54, John Holmes wrote: [EMAIL PROTECTED] wrote: hi, What is best method(if it's possible) to count how many times an object is instanced in one time script call? I means that if i have a class named "Test", i what to know how many times she's called, how many copies exists etc. The idea is for monitoring in the way to optimizing the code. The method get_declared_classes() only shows only the classes included/required. There's no predefined variable or method for determining this, that I'm aware of, short of counting how many "new Test" lines you have. You could write a wrapper class for Test that kept count of the instances and returned a new object upon request... In PHP5: class Foo { static $instances = 0; function __construct() { Foo::$instances++; } function __destruct() { Foo::$instances--; } static function getNumInstances() { return Foo::$instances; } } $foo = new Foo(); $fee = new Foo(); echo 'Count: '.Foo::getNumInstances()."\n"; unset( $foo ); echo 'Count: '.Foo::getNumInstances()."\n"; Add this to the class as well: public function __clone () { self::$instances++; // You might as well use the "self" operator } -- Daniel Schierbeck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
Curt Zirzow wrote: * Thus wrote Robert Cummings: On Mon, 2004-08-23 at 20:54, John Holmes wrote: [EMAIL PROTECTED] wrote: You could write a wrapper class for Test that kept count of the instances and returned a new object upon request... In PHP5: class Foo { static $instances = 0; function __construct() { Foo::$instances++; } function __destruct() { Foo::$instances--; } And: function __clone() { Foo::$instances++; } Curt Ooops, didn't see your post, hehe... By the way, use "self" instead of "Foo", just in case you change the class' name. -- Daniel Schierbeck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
Thanks, But im using php 4.3. Quoting Curt Zirzow <[EMAIL PROTECTED]>: > * Thus wrote Robert Cummings: > > On Mon, 2004-08-23 at 20:54, John Holmes wrote: > > > [EMAIL PROTECTED] wrote: > > > > > > You could write a wrapper class for Test that kept count of the > > > instances and returned a new object upon request... > > > > In PHP5: > > > > > > > class Foo > > { > > static $instances = 0; > > > > function __construct() > > { > > Foo::$instances++; > > } > > > > function __destruct() > > { > > Foo::$instances--; > > } > > And: > function __clone() > { > Foo::$instances++; > } > > > Curt > -- > First, let me assure you that this is not one of those shady pyramid schemes > you've been hearing about. No, sir. Our model is the trapezoid! > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- This mail sent through Horde-Toaster (http://qmailtoaster.clikka.com/) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to count objects instances
* Thus wrote [EMAIL PROTECTED]: > Thanks, > But im using php 4.3. Then you'll have to resort to some very unstandard methods: class foo { var $instance_manager; function getInstanceNum() { return $this->instance_manager->instancesOf(__CLASS__); } function getInstanceName() { return __CLASS__; } } class Factory { var $instances; function &create($name) { $this->instances[$name]++; // keep track of instances. $new = new $name(); // create object $new->instance_manager = &this; // tell instance about me return &$new;// return value. } function destroy(&$obj) { $this->instances[$this->getInstanceName()]--; unset($obj); } function instancesOf($name) { return $this->instance[$name]; } } $Factory = new Factory(); $o = &$Factory->Crate('foo'); echo $o->getIntanceNum(); $Factory->destroy($o); btw, totally untested. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP Runtime Debugger (Was: [PHP] how to count objects instances)
> There's no predefined variable or method for determining this, that I'm > aware of, short of counting how many "new Test" lines you have. > > You could write a wrapper class for Test that kept count of the > instances and returned a new object upon request... A slightly more general question: are there any PHP development environments that let you do something like a core dump, to see what kind of memory usage or variable allocation you have going on? Maybe a compilation option to PHP? A search of google turns up DBG and some commercial packages, but I lack the experience with such tools to evaluate them intelligently. I've done something like what the OP is asking about, by adding a log() feature and calling it when objects are instantiated or destroyed, but it sure would be nice to see something like this built-in. I'm a gdb beginner, but I'm willing to learn. I realize that this is a fairly complex problem compounded by the fact that PHP is generally a library used within an apache child process. Any thoughts? - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php