We all know PHP isn’t perfect, but even with just a few new features, it makes PHP all the more easier to use. With that being said, this thread is for all of us to put in our 2 cents on what kind of features we would love to have in PHP if it were ever possible.
I’ll start.
So ever since I’ve used other languages, I’ve been obsessed with the idea of List
data types or List
objects. Since PHP is starting to head more and more towards an OOP approach with every new update, I feel like PHP should introduce List
data types. This would allow users to create plural and singular objects.
So the usage I am thinking of would be something similar to other languages that use List
data types.
<?php
class Author {
public function __construct(public string $first_name = '', public string $middle_name = '', public string $last_name = '') {}
}
You create a singular class with properties and methods. So in the example above, we create an Author
class and use PHP 8’s new constructor promotion feature.
Then you’d use the List
keyword to create your List
object. Something like this.
List<Author> $authors = new List<Author>();
Our variable $authors
should now have all the features (add, remove, count, etc) a typical List
object should have since we inherit from a List
object. So the final usage would look something like this.
<?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'));
// Outputting the $authors list object would give you a list of authors as such
// List <Courtney Peppernell>, <William Shakespeare>, <Claudia Rankine>, <Jacqueline Woodson>, <Edgar Allan Poe>
print_r($authors);
In the above example, we manually added the new Author objects to the list, but you can do that dynamically too if you want.
So give me your 2 cents on features you want PHP to have or maybe even making my List
object example better.