PHP-Versions & features

PHP-Versions & features

PHP 8.2 New Features :

  1. Constructor Property Promotion in Traits: Allows properties to be initialized in traits constructors.
  2. New array_is_list() Function: Checks if an array is a list.
  3. Enums Now Support Union Types: Enums can now have union types.
  4. Saner Numeric String Comparisons: Improves the consistency of numeric string comparisons.
  5. fibers Extension Now Stable: The fibers extension is now marked as stable.
  6. splat Operator for Array Construction: Provides a concise way to build arrays.
  7. final Trait Methods: Allows declaring trait methods as final.

PHP 8.1 New Features :

  • Enumerations: Define a set of named constants representing discrete values.
  • Readonly properties: Declare class properties as readonly to prevent them from being modified after initialization.
  • Intersection types: Combine multiple types into one.
  • Fibers: Lightweight, cooperative multitasking units.
  • New password hashing functions: Argon2id-based password hashing functions.
  • Final class constants: Declare constants in classes as final to prevent overriding.

PHP 8.0 New Features:

  • Named Arguments: Allows passing arguments to a function by specifying the parameter name, improving code readability and flexibility.
  • Union Types: Enables declaring more than one possible type for a parameter, return value, or property.
  • Nullsafe Operator: Allows chaining method calls or accessing properties on potentially null values without causing errors.
  • Match Expression: A simpler and more concise alternative to switch statements for value comparison.
  • Constructor Property Promotion: Simplifies class property initialization by combining property declaration with constructor definition.

// **Named Arguments**
greet(age: 25, name: "John");

// **Union Types**
function square(int|float $num): int|float {

// **Nullsafe Operator**
$avatar = $user?->getProfile()?->getAvatar();

// **Match Expression**
$result = match($grade) {
    'A' => 'Excellent',
    'B' => 'Good',
    'C' => 'Average',
    default => 'Fail',
};

// **Constructor Property Promotion**
public function __construct(public int $x, public int $y) {}
        

PHP 7.4 New Features:

  • Typed Properties.
  • Arrow Functions.
  • Null Coalescing Assignment Operator (??=).
  • Spread Operator in Array Expression ([...$array]).

<?php
// Typed Properties
class MyClass {
    public int $counter = 0;
}

// Arrow Functions
$addOne = fn($x) => $x + 1;
$result = $addOne(5); // Result will be 6

// Null Coalescing Assignment Operator
$value ??= 'default';

// Spread Operator in Array Expression
$mergedArray = [...$array1, ...$array2];
        

PHP 7.3 New Features:

  • Flexible Heredoc and Nowdoc Syntax.
  • JSON_THROW_ON_ERROR option for json_encode() and json_decode().
  • is_countable() function.
  • Trailing Commas in Function Calls.

<?php
// Flexible Heredoc and Nowdoc Syntax
$str = <<<HTML
<div>
    Hello, $name!
</div>
HTML;

// JSON_THROW_ON_ERROR
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);

// is_countable()
if (is_countable($array)) {
    $count = count($array);
}

// Trailing Commas in Function Calls
function example(
    int $a,
    string $b,
) {
    // function body
}
        

PHP 7.2 New Features:

  • Object Type Hinting.
  • Parameter Type Widening.
  • Object Return Type Declarations.
  • Support for trailing commas in function calls.

<?php
// Object type hinting
function foo(MyClass $obj) {
    // $obj must be an instance of MyClass
}

// Parameter type widening
function bar(string ...$args) {
    // $args is an array of strings
}

// Object return type declarations
function createObject(): MyClass {
    return new MyClass();
}

// Trailing commas in function calls
function example(
    int $a,
    string $b,
) {
    // function body
}
        

PHP 7.1 New Features:

  • Nullable Types.
  • Void Return Type.
  • Iterable pseudo-type.
  • Square bracket syntax for array destructuring assignment.

<?php
// Nullable types
function foo(?int $bar): ?int {
    return $bar;
}

// Void return type
function log(string $message): void {
    file_put_contents('log.txt', $message, FILE_APPEND);
}

// Iterable pseudo-type
function process(iterable $items) {
    foreach ($items as $item) {
        // process item
    }
}

// Array destructuring assignment
[$a, $b] = [1, 2];
        

PHP 7.0 New Features:

  • Scalar Type Declarations (int, float, string, bool).
  • Null Coalescing Operator (??).
  • Spaceship Operator (<=>).

<?php
// Scalar type declarations
function add(int $a, int $b): int {
    return $a + $b;
}

// Null coalescing operator
$name = $_GET['name'] ?? 'Guest';

// Spaceship operator
$result = $a <=> $b;
        
Some Nath Kundu

Startup Enthusiastic || python, PHP, Node || Laravel, CodeIgniter, CakePHP, Symfony, Zend || AWS, GCP, Socket, unit-test || OOAD, Design Pattern, TDD, Agile

10 个月

CFBR

回复

要查看或添加评论,请登录

Som K的更多文章

  • Laravel Middleware

    Laravel Middleware

    Laravel Middleware is a feature that provides a convenient mechanism for filtering HTTP requests entering your…

    1 条评论
  • Laravel-Job-Queue

    Laravel-Job-Queue

    Laravel's Job Queue is a powerful feature that allows you to defer the processing of time-consuming tasks, such as…

    1 条评论
  • Laravel-Notifications

    Laravel-Notifications

    Step 1: Step 2: Step 3 Trigger Notifications: You can trigger these notifications when a payment is successful for an…

  • laravel-Passport-OAuth

    laravel-Passport-OAuth

    OAuth (Open Authorization) is an open standard for access delegation, commonly used to enable secure access to…

    1 条评论
  • Service Provider & Container

    Service Provider & Container

    Laravel's Service Provider vs. Service Container: Service Container: The service container is a fundamental part of…

  • Laravel-Event-System

    Laravel-Event-System

    Laravel's event system allows for easy observation and handling of events throughout the application. Events can be…

  • Laravel-Task-Scheduler

    Laravel-Task-Scheduler

    Laravel's Task Scheduling feature allows you to schedule Artisan commands within your Laravel application. This is…

    1 条评论
  • Design Patterns

    Design Patterns

    Explain Factory Pattern The Factory pattern is used to create objects without specifying the exact class of the object…

    1 条评论

社区洞察

其他会员也浏览了