I'm tired of reading the same message all time: "PHP sucks."
But most of these critics haven't looked at PHP since 2012, and a lot has changed since then.
Let's take a look at the language changes that have happened since PHP 5.4 was released.
Table of contents:
PHP 5.4 introduced traits, which allow for composition over inheritance. You can have traits and include them in every class.
trait SayWorld {
public function sayHello() {
echo 'World!';
}
}
class MyHelloWorld {
use SayWorld;
}
Gone are the days of writing array()
like a caveman. You can now use square brackets for short array syntax.
$newArray = [
'first',
'second',
'third'
];
Assigning an array to a temporary variable is a thing of the past. You can now use array destructuring to directly assign variables from an array.
$newArray = [
'first',
'second',
'third'
];
$anotherArray = [
...$newArray,
'fourd',
'fifth'
];
You can pass as many arguments to a function as you want using the ...
syntax.
class Variadic {
public function query($query, ...$attributes) {
var_dump($query, $attributes);
}
}
$v = new Variadic();
$v->query('select * from users', 1, 'Víctor');
// result 'select * from users', 1, 'Víctor'
Need to do something memory-intensive in a memory-efficient way? Generators are the way to go.
Here you can find more info about generators.
Need a new class but can't be bothered to make a new file? Anonymous classes are the solution. They can implement an interface just like any other class.
$anonymousClass = new class{
public $property = 1;
public function add(int $x): int
{
return $this->property + $x;
}
}
echo $anonymousClass->propert; // 1
echo $anonymousClass->add(2); // 3
No more worrying about adding a trailing comma to a function call or method call.
class TrailingComma {
public function action(
int $x,
int $y, // <- This comma causes an error before
) {
echo $x + $y;
}
}
PHP has arrow functions too! They're not exactly like JavaScript's, but they're a great addition to the language.
$y = 1;
$fn1 = fn($x) => $x + $y;
$x = $fn1(2); // 3
No more checking for null before assigning a value. The null coalescing operator has got you covered.
// If user is null or undefined, 'nobody' is assigned
$username = $_GET['user'] ?? 'nobody';
And if you need to shorthand that null coalescing operator, there's an assignment operator for that too.
// Value of $_GET['user'] change if is undefined or null
$_GET['user'] ??= 'nobody';
No more checking for null before calling a method. The null chaining operator is here to save the day.
class Customer {
public function getAddress(): ?Address {}
}
class Address {
public function getCountry(): string {}
}
// Null if no Address
$country = $customer->getAddress()?->getCountry();
Sick of using null
to skip over optional arguments? Named arguments are the solution.
Imagine that you have a function with a lot of arguments, some or them are nullable.
function takes_many_args(
$first_arg,
$second_arg = 'any',
$third_arg = 5,
)
{
// ...
}
Before you have to send the default $second_arg, even if you are not changing it.
takes_many_args('first', 'any', 3);
Now you can just do something like this:
takes_many_args('first', third_arg: 3);
No more switch statements that are a mile long. The match statement is a more compact and readable way to write switch statements.
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
default => 'Unknown food',
};
var_dump($return_value); // 'This food is a cake'
Enums are finally here! You can create enum classes with values and methods, and even use them as type hints.
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
function do_stuff(Suit $s)
{
// ...
}
do_stuff(Suit::Spades);
PHP now has typed arguments, return types, union types, intersection types, and more. You can even use type hints for enums!
Now we can add types to arguments, return values, class properties, constants, and almost anything.
Gone are the days of verbose constructors. Constructor property promotion is here to reduce boilerplate code.
This was one of the best additions to PHP, now classes are super simple and we don't need to write classess properties more than once.
class Customer
{
public function __construct(
public string $name,
public string $email,
) {}
}
And now we don't need to create custom getters and setters and set private visibility to each method.
With the readonly
flag we can secure our properties from modifications outside of the class itself.
class Customer
{
public function __construct(
readonly public string $name,
readonly public string $email,
) {}
}
In PHP 8.3 they added anonymous readonly classes.
$class = new readonly class {
public function __construct(
public string $name = 'Víctor',
) {}
};
PHP has experienced a 400% performance increase between 5.6 and 7, and another 20% between 7 and 8.
It's fast enough for most use cases, and if you need a specialized use case, use a specialized language.
I have manage more than +725M request per week while working at Wallbox.
In conclusion, PHP is not dead, nor is it sucking anymore. The language has undergone significant changes since 2012, and it's high time to revise our opinions about it.
With the introduction of traits, short array syntax, array destructuring, and a host of other features, PHP has become a more efficient, readable, and maintainable language.
Add to that the improvements in error handling, the introduction of attributes, and the long-awaited arrival of enums, and it's clear that PHP has evolved into a robust and reliable choice for web development.
So, the next time someone tells you that PHP sucks, you can confidently tell them that they're just stuck in the past.
Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.
Get 1 month free with yearly plan during checkout.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.