Hello Niklas,
This something very pertinent that you noticed. Thanks you for pointing it out. Q - “sounds like everything you want is typed arrays” A- It’s much more than simple typed arrays. I forgot to mention that those “Collection” would be passed, like objects, by reference. Meaning to a memory-optimized type as it does not make local copies of the variable. Q – “What's wrong with having it as object as long as it's implemented efficiently” A – 1 It’s semantically much more appropriated. Collections of data is becoming something actively used in php since those frameworks are born all over. 2 An object IMO is not intended to be used as “array of others object”. It’s a design issue following a lack of this variable type in php. 3 It’s native to php, meaning that’ll be faster as its core-optimized, especially with huge Collection of data. Doctrine users will understand this sentence. 4 This will allows frameworks to make use of a native Collection, instead of doing their own ones. Nowadays each framework are using their one Collection object which is not standardized at all since they all make the same use of the Collection. Now I agree with something: This will be.a.huge.work behind the scenes. Not only for Php developpers, but also for testers, documenters, etc. This could be a major point of interest for Php8, lot of things moved in the good way since Php 7. We can keep going by improving the code's semantic and evolve in the way of framework makers. Cheers, Georges De : Niklas Keller [mailto:m...@kelunik.com] Envoyé : samedi 28 janvier 2017 17:10 À : georges <geol...@gmail.com> Cc : PHP Internals <internals@lists.php.net> Objet : Re: [PHP-DEV] Fwd: Type Collection 2017-01-28 16:37 GMT+01:00 georges <geol...@gmail.com <mailto:geol...@gmail.com> >: 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 Morning, sounds like everything you want is typed arrays? Or how does it differ from typed arrays? What's wrong with having it as object as long as it's implemented efficiently? Regards, Niklas