The PHP we all deserve (Fantasy features)

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.

4 Likes

What you’re describing is called Generics and yes I’d really like that too, along with a lot of other PHP developers :slight_smile:

What I would personally really like is method overloading. That way you can have multiple methods with the same name, but different arguments. Which method is actually ran depends on the arguments passed to it.

class CommandHandler
{
    public function handle(Foo $command): void
    {
    }
    public function handle(Bar $command): void
    {
    }
}

$handler = new CommandHandler();
$handler->handle(new Foo()); // first method is called
$handler->handle(new Bar()); // second method is called
2 Likes

Method overloading is a good one.

Thoughts on Asynchronous PHP? Don’t know if there’s already a library for that, but Asynchronous PHP would be pretty beneficial as well.

I’m not familiar with the concept of list objects in a language. I’m sure I’m over-simplifying the idea by thinking this, but to me it seems like an array, which we are familiar with. How do lists differ from an array?
Can you create a list class to do the things you want a list to do?
I fact, I have previously created classes that hold an array for lists of other objects. But since I’m not familiar with lists in the context you mean, it could be something completely different.

1 Like

The advantage is you can add methods that can handle common tasks on a list. Such as searching for a value by some property, or merging two lists together in some way, or removing a specific element identified by a specific criterion, or, or …

Sure, you can do all those things with arrays too, but with objects it’s generally easier to read.

For example:

$list->findById($someId);

as opposed to

$found = null;
foreach ($items as $item) {
    if ($item->id() === $someId) {
       $found = $item;
       break;
    }
}

both do the same, but the first one is way more clear.

Of course, this gets better the more complex the logic gets.

As a bonus, those methods on lists are really easy to write tests for, to get certainty that they do what they need to do as an isolated operation, rather than as a part of some big whole.

2 Likes

I might be butchering this since I’m still new to other OOP languages, but List objects have built-in methods you don’t have to write. Those come automatically, you don’t need an external function or write a method to do those things that is already inherited by a List. So for example, say you want to count the number of elements that are in that list. Well, if we’re in PHP and we want to count, we’d just use the count() function. In other languages, you’d have to use for loops to get the number of elements if we’re using arrays. Whereas with Lists, to get the count of the List, you’d just do object->count or object.count and you automatically get the count without writing your own code to do that math. You also inherit not just from a List object, but also from the data type you are using for your variable as well. So if the Author class has methods and properties, all of that also gets included as well.

I maybe over complicating it a bit. Essentially, yes both Lists and arrays do have similarities, but they are also so different. They are both containers, but arrays can only do so much as opposed to Lists.

I think this part is just more of a personal preference (view) thing, but PHP is starting to move more and more towards OOP like I said earlier. More and more PHP functions are being replaced by OOP versions, e.g. money_format is now replaced with the NumberFormatter class. If PHP is heading this way, what’s to say the rest of PHP’s procedural functions won’t be removed? Functions like count() could be replaced for something more beneficial that’s written in OOP.

1 Like