Fwd: Type Collection
| From: | georges | Date: | Sat, 28 Jan 2017 15:37:26 +0000 |
| Subject: | Fwd: Type Collection | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98004@lists.php.net to get a copy of this message | ||
Hello
Php Internals,
I was wondering today why did we not have (yet ?)
a Collection type in Php ?
Typically what would be that kind of variable type ?
This type would be a great intermediate between an array and an Object.
Today, in my opinion we are seeing to much wrong uses of php Object.
To quote one of the most used: The Doctrine Collection object.
These kind of object are IMO very memory-greedy ones.
The implementation idea would be to have a new variable type:
The Collection.
The Collection is Iterable, Traversable, Countable like an array to be
compatible with foreach and other Traversable-compatibles functions.
The Collection become a type on it's
own, so there's a lot of changes to be considered:
- The serialization
- The vardumping/exporting
- The Exception traces
- The backtrace
To be syntaxely compliant to array and object it would be case insensitive
even if i would like to prefer it Camelcased: Collection. This is up to you..
The target is Php 8, but the reserved word could be introduced in php 7.3.
Here's are very basics implementation concepts:
/**
* Standard var declaration
*/
$collection = Collection(int);
/**
* Alternative var declaration
*/
$collection = Collection(int, [1, 2, 3]);
// Throws nothing
$collection[]= 10;
$collection['abc']= 10;
// Throws CollectionTypeHintError
$collection = Collection(int, [1, '2', 3]);
$collection['abc']= '10';
$collection['abc']= new StdClass;
// Transtypes the collection to an standard array
$collectionAry = (array) $collection;
// Throws CollectionTypeReHintingError
$collection = (array) $collection;
$collection = 10;
// Throws nothing
unset($collection);
$collection = 10;
Now the +/-;
The +:
+ We have an optimized Collection type instead of those ugly Object-like
collections that are ressources-greedy.
+ Aside of the optimization, we have a well understandable type hint
reserved to the Collections. This is clearly a better
semantically-appropriated way to work with Collection data.
The -:
- Collection become a reserved word. Therefore, this could introduce a
major BC.
Cheers,
Georges.L