rfc:instance_counter
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionLast revision | |||
— | rfc:instance_counter [2013/05/08 07:28] – [PHP RFC: instance counter] Declined frank | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== PHP RFC: instance counter ====== | ||
+ | * Version: 0.4 | ||
+ | * Date: 2013-04-08 | ||
+ | * Author: Frank Liepert, contact@frank-liepert.de | ||
+ | * Contributor: | ||
+ | * Status: Declined | ||
+ | * First Published at: http:// | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | Coming to Object-Orientated Programming, | ||
+ | |||
+ | 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 ===== | ||
+ | |||
+ | 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/ | ||
+ | |||
+ | The function name should fit in with the current names of class/ | ||
+ | |||
+ | * **If no argument is provided, the number of all objects in the object store as an associative array (' | ||
+ | <code php> | ||
+ | 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 () | ||
+ | </ | ||
+ | |||
+ | * **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(' | ||
+ | // 0 | ||
+ | |||
+ | $foo = new stdClass; | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 1 | ||
+ | |||
+ | $bar = new stdClass; | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 2 | ||
+ | |||
+ | $bar = null; | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 1 | ||
+ | |||
+ | $foo = null; | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 0 | ||
+ | </ | ||
+ | |||
+ | * **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 | ||
+ | </ | ||
+ | |||
+ | * **If an an array is provided, it will be the treated as an inclusive indexed array of class names. An associative array (' | ||
+ | <code php> | ||
+ | print_r (get_objects_count(array(' | ||
+ | // Array ( [stdClass] => 0 ) | ||
+ | |||
+ | $foo = new stdClass; | ||
+ | |||
+ | print_r (get_objects_count(array(' | ||
+ | // Array ( [stdClass] => 1 ) | ||
+ | |||
+ | $bar = new stdClass; | ||
+ | |||
+ | print_r (get_objects_count(array(' | ||
+ | // Array ( [stdClass] => 2 ) | ||
+ | |||
+ | $bar = null; | ||
+ | |||
+ | print_r (get_objects_count(array(' | ||
+ | // Array ( [stdClass] => 1 ) | ||
+ | |||
+ | $foo = null; | ||
+ | |||
+ | print_r (get_objects_count(array(' | ||
+ | // Array ( [stdClass] => 0 ) | ||
+ | </ | ||
+ | |||
+ | ===== General questions & answers ===== | ||
+ | |||
+ | ==== Inheritance ==== | ||
+ | |||
+ | On internals list there was the question, if only the " | ||
+ | <code php> | ||
+ | class A {} | ||
+ | class B extends A {} | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 0 | ||
+ | |||
+ | $b = new B; | ||
+ | |||
+ | var_dump($b instanceof A); | ||
+ | // bool(true) | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 0 | ||
+ | |||
+ | print get_objects_count(' | ||
+ | // 1 | ||
+ | </ | ||
+ | |||
+ | ===== Use cases: ===== | ||
+ | * Debugging | ||
+ | * Implementation of design patterns (f.ex. flyweight pattern) | ||
+ | * Extended control: limit the number of objects | ||
+ | * Teach people about assignment of objects | ||
+ | * to be continued... | ||
+ | |||
+ | ===== Function name ===== | ||
+ | |||
+ | Since there were no objections against the proposed function name, its status changes to accepted. | ||
+ | < | ||
+ | // Proposed + Accepted | ||
+ | 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 | ||
+ | |||
+ | ===== SAPIs Impacted ===== | ||
+ | |||
+ | None. | ||
+ | |||
+ | ===== Impact to Existing Extensions ===== | ||
+ | |||
+ | None. | ||
+ | |||
+ | ===== New Constants ===== | ||
+ | |||
+ | None. | ||
+ | |||
+ | ===== php.ini Defaults ===== | ||
+ | |||
+ | None. | ||
+ | |||
+ | ===== Open Issues ===== | ||
+ | |||
+ | None. | ||
+ | ===== Patches and Tests ===== | ||
+ | |||
+ | Implementation: | ||
+ | |||
+ | Tests need to be done. | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | * https:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | |||
+ | ===== Rejected Features ===== | ||
+ | |||
+ | - None. | ||
+ | |||
+ | ===== Vote ===== | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | | ||
+ | ==== Voting period ==== | ||
+ | Start: **2013/ | ||
+ | |||
+ | End: **2013/ |
rfc/instance_counter.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1