New PHP 8.x Functions

Exploring New Functions in PHP 8.x

  • Overview of PHP 8.x
    • PHP 8.0 released in November 2020, followed by subsequent 8.x versions.
    • Focus on improving performance, introducing new features, and enhancing developer experience.
  • Significance of PHP 8.x
    • Major updates in language features.
    • Better performance and security improvements.

New Features in PHP 8.0

  • str_contains()
    • Checks if a string contains a substring.
echo str_contains('Hello World', 'World'); // true
  • str_starts_with() and str_ends_with()
    • Checks if a string starts or ends with a given substring.
echo str_starts_with('Hello World', 'Hello'); // true
echo str_ends_with('Hello World', 'World'); // true
  • get_debug_type()
    • Returns the type of a variable, improving upon gettype().
echo get_debug_type([]); // 'array'

New Features in PHP 8.1

  • Readonly Properties
    • Properties that can only be initialized once..
class Test {
    public readonly string $name;
}
  • Enums
    • A way to define a set of possible values for a variable.
enum Status {
    case Pending;
    case Completed;
}
  • Fibers
    • Low-level mechanism for implementing cooperative multitasking.
$fiber = new Fiber(function() {
    echo "Fiber!";
});
$fiber->start();

New Functions in PHP 8.1

  • array_is_list()
    • Checks if an array is a list.
array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true

// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false

// The keys are not in the correct order
array_is_list([1 => 'apple', 0 => 'orange']); // false

// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false

// Non-consecutive keys
array_is_list([0 => 'apple', 2 => 'bar']); // false
  • enum_exists()
    • Checks if an enum exists..
// Check that the enum exists before trying to use it
if (enum_exists(Suit::class)) {
    $myclass = Suit::Hearts;
}

New Features in PHP 8.2

  • Read-only Classes
    • All properties in a class are readonly.
class ItemDTO
{
    public function __construct(
        public readonly string $title,
        public readonly Status $status,
        public readonly ?DateTimeImmutable $published_at = null,
    ) {}
}
  • Disjunctive Normal Form (DNF) Types
    • Complex type declarations using intersection and union types.
function respond(): (JSONResponse&SuccessResponse)|HTMLResponse|string {}

function generateSlug((HasTitle&HasId)|null $post) 
{
    if ($post === null) {
        return '';
    }

    return 
        strtolower($post->getTitle()) 
        . $post->getId();
}

New Functions in PHP 8.2

  • json_validate()
    • Validates a JSON string without decoding.
<?php
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
var_dump(json_validate('{ "": "": "" } }')); // false
?>

New Features in PHP 8.3

  • getBytesFromString()
    • select bytes from the string at random until it reaches that specified length
echo str_truncate("Hello World", 5); // Hello...
  • #[\Override] attribute
class A {
    protected function ovrTest(): void {}
}

// This will work because ovrTest() 
// can be found in the parent class
class B extends A {
    #[\Override]
    public function ovrTest(): void {}
}

// This will fail because ovrBest() 
// (probably a typo) is not in the parent
class C extends A {
    #[\Override]
    public function ovrBest(): void {}
}
  • getFloat(), nextFloat
    •  generate random float values
$rando = new Random\Randomizer();

// Generate a float value between a minimum 
//  value of 0 and a maximum value of 5
$rando->getFloat(0,5); // 2.3937446906217

//
$rando = new Random\Randomizer();

$rando->nextFloat(); // 0.3767414902847
$rando = new Random\Randomizer();
$alpha = 'ABCDEFGHJKMNPQRSTVWXYZ';

$rando->getBytesFromString($alpha, 6); //  "MBXGWL"
$rando->getBytesFromString($alpha, 6); //  "LESPMG"
$rando->getBytesFromString($alpha, 6); //  "NVHWXC"

$rando = new Random\Randomizer();
$weighted = 'AAAAA12345';

$rando->getBytesFromString($weighted, 5); //  "1AA53"
$rando->getBytesFromString($weighted, 10); //  "42A5A1AA3A"

Performance Improvements

  • JIT Enhancements
    • Improved Just-In-Time compilation.
    • Better performance for complex calculations and loops.
  • Memory Usage Optimization
    • Reduced memory overhead for certain operations.

Deprecated Features and Backward Compatibility

  • Deprecated Functions
    • List deprecated functions and alternatives.
    • Example: each(), create_function(), etc.
  • Backward Compatibility
    • Efforts to maintain compatibility.
    • Efforts to maintain compatibility.

Q&A: Questions from the audience

new PHP 8+ features.

By TenantCloud

new PHP 8+ features.

new PHP 8+ features.

  • 116