PHP 8 New Features

Choirul Anam
3 min readApr 13, 2022

--

Learning from UDEMY

Photo by Shahadat Rahman on Unsplash

Assalamualaikum warrahmatullahi wabarakatuh.

Comma in Parameter List

This is a simple but useful feature, in PHP 8 we can now add a comma at the end of a parameter list, such as when calling functions to create arrays and so on.

for more details can see the php wiki
https://wiki.php.net/rfc/trailing_comma_in_parameter_list
https://wiki.php.net/rfc/trailing_comma_in_closure_use_list

comma in parameters

function sayHello(string $first, string $last)
{

}

function sum(int... $values){

}

sayHello(
"Choirul",
"Choirul",
"Choirul",
"Choirul",
);

sum(
1,
1,
1,
1,
1,
);

comma in Array

$array = [
"first" => "choirul",
"mid" => "anam",
"last" => "keren",
];

Non Capturing Catches
when an error occurs in PHP, usually we will use try catch, then in catch we will catch the error and store it in an exception variable even though we don’t actually use it we still have to create the exception variable, in PHP 8 now we are not obliged to create the exception variable if it is not will use it.

<?php

function validate(string $values){
if(trim($values) == "") {
throw new Exception("Invallid string");
}
}

try{
validate(" ");
}catch (Exception) {
echo "Invalid" . PHP_EOL;
}

Throw Expression
throw is a statement in PHP 7, while in PHP 8 throw is an expression.

<?php


function sayHello(?string $name){

if($name == null){
throw new Exception("Tidak boleh null");
}

echo "Hello $name" . PHP_EOL;
}

$name = "choirul";

function sayHelloExpression(?string $name)
{
$result = $name != null ? "Hello $name" : throw new Exception("tidak boleh null") ;

echo "Hello $name" . PHP_EOL;
}

sayHelloExpression(null);
sayHelloExpression("choirul");

Allow::class on object

in PHP 8 now we can directly take the class name from $object::class directly for more details can be seen on the PHP wiki https://wiki.php.net/rfc/class_name_literal_on_object.

<?php

class Login {

}

$login = new Login();

var_dump($login::class);
var_dump(get_class($login));
var_dump(Login::class);

Stringable Interface
In PHP 8, a new interface called Stringable is introduced, if we override the magic function __toString, our class will automatically implement the Stringable interface, we don’t need to do this manually in PHP.

<?php

function sayHello(Stringable $stringable): void
{
echo "Hello {$stringable->__toString()}" . PHP_EOL;
}

// override string

class Person {

public function __toString(): string
{
return "Person";
}
}

sayHello(new Person);

for more details go to https://wiki.php.net/rfc/stringable.

Override toString function

New String Function
In PHP 8 there is a feature to manipulate strings

<?php

// mengecek content
var_dump(str_contains("Choirul Anam", "Anam"));
var_dump(str_contains("Choirul Anam", "Choirul"));
var_dump(str_contains("Choirul Anam", "choirul"));

// diawal
var_dump(str_starts_with("Choirul Anam", "Choirul"));
var_dump(str_starts_with("Choirul Anam", "Anam"));
var_dump(str_starts_with("Choirul Anam", "choirul"));

// akhiran
var_dump(str_ends_with("Choirul Anam", "Choirul"));
var_dump(str_ends_with("Choirul Anam", "Anam"));
var_dump(str_ends_with("Choirul Anam", "choirul"));

thats for today. Thanks

Wassalamualaikum warrahmatullahi wabarakatuh.

--

--

Choirul Anam
Choirul Anam

No responses yet