This is an old revision of the document!
PHP RFC: instance counter
- Version: 0.1
- Date: 2013-04-08
- Author: Frank Liepert, email@example.com
- Contributor: Joe Watkins
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/instance_counter
Coming to Object-Orientated Programming, PHP offers a variety of possibilities to obtain information about classes and their instances (objects). In this regard, the classes/objects functions (http://www.php.net/manual/en/ref.classobj.php) as well as the reflection API (http://www.php.net/manual/en/book.reflection.php) do a great job. But still there is no function to obtain a particular information: the number of instances of a class.
One might say, simply implement a static counter in each class. But what about built-in classes like SPL? A wrapper would be required. Implementing it these ways, it does not only sound, but it actually is cumbersome. The purpose of this RFC is to address this issue.
The proposal is to add a new function returning an array ('class name' ⇒ number of instances) in the current script.
The function name should fit in with the current names of class/object functions. Therefore, the name get_objects_count() seems to be reasonable, but is still up for discussion (see Function name).
print_r (get_objects_count()); // Array () $foo = new stdClass; print_r (get_objects_count()); // Array ( [stdClass] => 1 ) $bar = new stdClass; print_r (get_objects_count()); // Array ( [stdClass] => 2 ) $bar = null; print_r (get_objects_count()); // Array ( [stdClass] => 1 ) $foo = null; print_r (get_objects_count()); // Array ()
- Implementation of design patterns (f.ex. flyweight pattern)
- Extended control: limit the number of objects
- to be continued…
Further suggestions for the function name in example #1:
// Proposed get_objects_count() // Alternatives get_instances_counts() get_instances_count() get_instance_counts() get_object_store_count() get_class_counts()
Backward Incompatible Changes
No BC breaks.
Proposed PHP Version(s)
next PHP 5.4.x or PHP 5.5.x
Impact to Existing Extensions
- Decision on function name.
Patches and Tests
A first implementation: https://gist.github.com/krakjoe/5275773
Tests need to be done.