php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80620 feature request - List data types
Submitted: 2021-01-13 05:08 UTC Modified: 2021-01-13 23:29 UTC
From: fazedoutofturn at hotmail dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
 [2021-01-13 05:08 UTC] fazedoutofturn at hotmail dot com
Description:
------------
This is my first time requesting for a PHP feature or using this report system. But since PHP 8 has been released, could we get List data types? We already have scalar data type declarations now. It would be nice to have other data types that other languages has as well. Arrays are nice, but having Lists would make PHP head towards a more consistent OOP language.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-13 06:54 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-01-13 06:54 UTC] requinix@php.net
See the proposal for the "array_is_list" function, and related messages on the internal mailing list.
https://wiki.php.net/rfc/is_list
 [2021-01-13 23:07 UTC] fazedoutofturn at hotmail dot com
@requinix

Well, what I mean is an actual List data type or a List object data type. Reading the link you gave me, it pertains to just arrays. So basically pluralizing objects. What I am talking about is something like this as an example (not working for obvious reasons).

------

<?php
class Author {

	public function __construct(public string $first_name = '', public string $middle_name = '', public string $last_name = '') {

	}

}

List<Author> $authors = new List<Author>();
$authors->add(new Author('Courtney', '', 'Peppernell'));
$authors->add(new Author('William', '', 'Shakespeare'));
$authors->add(new Author('Claudia', '', 'Rankine'));
$authors->add(new Author('Jacqueline', '', 'Woodson'));
$authors->add(new Author('Edgar', 'Allan', 'Poe'));

// Would give you a list of authors as such
// List <Courtney Peppernell>, <William Shakespeare>, <Claudia Rankine>, <Jacqueline Woodson>, <Edgar Allan Poe>
print_r($authors);

------

Since PHP has already adopted "some" of Java's OOP, why can't we just adopt everything? Most languages have List data types so why can't PHP? I know people will say arrays are enough, but having a consistency in OOP would help people move around languages to languages more. Plus, it sort of seems PHP is heading that way anyways where everything is more strictly typed.
 [2021-01-13 23:29 UTC] requinix@php.net
Sounds like you want SplDoublyLinkedList.
https://www.php.net/manual/en/class.spldoublylinkedlist.php

Queue or Stack could be more appropriate for your use case.
https://www.php.net/manual/en/class.splqueue.php
https://www.php.net/manual/en/class.splstack.php
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 13:03:32 2021 UTC