rfc:instance_counter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:instance_counter [2013/04/09 10:13]
frank [Use cases:]
rfc:instance_counter [2017/09/22 13:28] (current)
Line 1: Line 1:
 ====== PHP RFC: instance counter ====== ====== PHP RFC: instance counter ======
  
-  * Version: 0.2+  * Version: 0.4
   * Date: 2013-04-08   * Date: 2013-04-08
   * Author: Frank Liepert, contact@frank-liepert.de   * Author: Frank Liepert, contact@frank-liepert.de
   * Contributor:​ Joe Watkins   * Contributor:​ Joe Watkins
-  * Status: ​Under Discussion+  * Status: ​Declined
   * First Published at: http://​wiki.php.net/​rfc/​instance_counter   * First Published at: http://​wiki.php.net/​rfc/​instance_counter
  
Line 12: Line 12:
 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. ​ 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.+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 not only sounds, but actually is cumbersome. The purpose of this RFC is to address this issue.
  
 ===== Proposal ===== ===== Proposal =====
  
-The proposal is to add a new functionality dealing with the number of instances of either a specific class or all classes.+The proposal is to add a new functionality dealing with the number of instances of either a specific class name/object or set of class names/​objects ​or all classes.
  
-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]]).+The function name should fit in with the current names of class/​object functions. Therefore, the name **get_objects_count()** seems to be reasonable.
  
-  * If a class name is provided, the number of objects of the specified class in the object store will be returned. +  * **If no argument ​is provided, the number of all objects in the object store as an associative array ('​class name' => count) will be returned.**
-  ​* If no class name is provided, the number of all objects in the object store as an associative array ('​class name' => count) will be returned. +
- +
-Example #1:+
 <code php> <code php>
 print_r (get_objects_count());​ print_r (get_objects_count());​
 // Array () // Array ()
-print get_objects_count('​stdClass'​);​ 
-// 0 
  
 $foo = new stdClass; $foo = new stdClass;
Line 34: Line 29:
 print_r (get_objects_count());​ print_r (get_objects_count());​
 // Array ( [stdClass] => 1 ) // Array ( [stdClass] => 1 )
-print get_objects_count('​stdClass'​);​ 
-// 1 
  
 $bar = new stdClass; $bar = new stdClass;
Line 41: Line 34:
 print_r (get_objects_count());​ print_r (get_objects_count());​
 // Array ( [stdClass] => 2 ) // Array ( [stdClass] => 2 )
-print get_objects_count('​stdClass'​);​ 
-// 2 
  
 $bar = null; $bar = null;
Line 48: Line 39:
 print_r (get_objects_count());​ print_r (get_objects_count());​
 // Array ( [stdClass] => 1 ) // Array ( [stdClass] => 1 )
-print get_objects_count('​stdClass'​);​ 
-// 1 
  
 $foo = null; $foo = null;
Line 55: Line 44:
 print_r (get_objects_count());​ print_r (get_objects_count());​
 // Array () // Array ()
 +</​code>​
 +
 +  * **If a class name is provided, the number of objects of the specified class in the object store will be returned.**
 +<code php>
 print get_objects_count('​stdClass'​);​ print get_objects_count('​stdClass'​);​
 // 0 // 0
 +
 +$foo = new stdClass;
 +
 +print get_objects_count('​stdClass'​);​
 +// 1
 +
 +$bar = new stdClass;
 +
 +print get_objects_count('​stdClass'​);​
 +// 2
 +
 +$bar = null;
 +
 +print get_objects_count('​stdClass'​);​
 +// 1
 +
 +$foo = null;
 +
 +print get_objects_count('​stdClass'​);​
 +// 0
 +</​code>​
 +
 +  * **If an object is provided, the number of objects of the specifiied objects class in the object store will be returned. The return value is always ≥ 1.**
 +<code php>
 +$foo = new stdClass;
 +
 +print get_objects_count($foo);​
 +// 1
 +
 +$bar = new stdClass;
 +
 +print get_objects_count($bar);​
 +// 2
 +
 +$bar = null;
 +
 +print get_objects_count($foo);​
 +// 1
 +</​code>​
 +
 +  * **If an an array is provided, it will be the treated as an inclusive indexed array of class names. An associative array ('​class name' => count) will be returned.**
 +<code php>
 +print_r (get_objects_count(array('​stdClass'​)));​
 +// Array ( [stdClass] => 0 )
 +
 +$foo = new stdClass;
 +
 +print_r (get_objects_count(array('​stdClass'​)));​
 +// Array ( [stdClass] => 1 )
 +
 +$bar = new stdClass;
 +
 +print_r (get_objects_count(array('​stdClass'​)));​
 +// Array ( [stdClass] => 2 )
 +
 +$bar = null;
 +
 +print_r (get_objects_count(array('​stdClass'​)));​
 +// Array ( [stdClass] => 1 )
 +
 +$foo = null;
 +
 +print_r (get_objects_count(array('​stdClass'​)));​
 +// Array ( [stdClass] => 0 )
 +</​code>​
 +
 +===== General questions & answers =====
 +
 +==== Inheritance ====
 +
 +On internals list there was the question, if only the "​direct"​ instances of a class or also the instances of subclasses are counted? The answer is: only direct instances. See the following code:
 +<code php>
 +class A {}
 +class B extends A {}
 +
 +print get_objects_count('​A'​);​
 +// 0
 +
 +$b = new B;
 +
 +var_dump($b instanceof A);
 +// bool(true)
 +
 +print get_objects_count('​A'​);​
 +// 0
 +
 +print get_objects_count('​B'​);​
 +// 1
 </​code>​ </​code>​
  
Line 68: Line 149:
 ===== Function name ===== ===== Function name =====
  
-Further suggestions for the function name in example #1:+Since there were no objections against ​the proposed ​function name, its status changes to accepted.
 <​code>​ <​code>​
-// Proposed+// Proposed ​+ Accepted
 get_objects_count() get_objects_count()
  
Line 107: Line 188:
 ===== Open Issues ===== ===== Open Issues =====
  
-  * Decision on function name. +None.
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
Line 124: Line 204:
  
 - None. - None.
 +
 +===== Vote =====
 +<doodle title="​Should this RFC be implemented?"​ auth="​frank"​ voteType="​single"​ closed="​true">​
 +   * Yes
 +   * No
 +</​doodle>​
 +    ​
 +==== Voting period ====
 +Start: **2013/​04/​30**
 +
 +End: **2013/​05/​07**
rfc/instance_counter.1365502417.txt.gz · Last modified: 2017/09/22 13:28 (external edit)