contact us<\/a>.<\/p>\n\n\n\nBefore we start, what is PHP 8.1?<\/h2>\n\n\n\n PHP is a programming language that is mainly adapted to web development, and on which WordPress is based. Its latest update, 8.1, contains a lot of new features, optimizations and performance improvements. <\/p>\n\n\n\n
Some of the most important new features<\/strong> are: <\/p>\n\n\n\n\nEnumerations<\/li>\n\n\n\n First-class Callable Syntax<\/li>\n\n\n\n The single-reading classes<\/strong>, which allows to have an immutable class, that is to say, that does not allow the modification of this after its creation. <\/li>\n\n\n\nNew expression in constructor<\/li>\n\n\n\n Pure intersection types<\/li>\n\n\n\n Fibers<\/li>\n\n\n\n Never return type<\/li>\n\n\n\n Final class constants<\/li>\n\n\n\n Explicit octal numeric notation<\/li>\n\n\n\n Array destructuring support<\/li>\n\n\n\n Performance improvements<\/li>\n\n\n\n New classes, interfaces and functions<\/li>\n<\/ul>\n\n\n\n
<\/figure><\/div>\n\n\n<\/div>\n\n\n\n
1. Enumerations<\/h3>\n\n\n\n An enum is a data type used to define possible values, such as “true” or “false”.<\/p>\n\n\n\n
In general, enums are a useful way to make our code cleaner, reduce errors and make it easier to read<\/strong>.<\/p>\n\n\n\nWith the PHP 8.1 update, this is no longer the case:<\/p>\n\n\n\n
{ class Status\n{\n const DRAFT = 'draft';\n const PUBLISHED = 'published';\n const ARCHIVED = 'archived';\n}\nfunction acceptStatus(string $status) {...}<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
{ enum Status.\n{\n case Draft;\n case Published;\n case Archived;\n}\nfunction acceptStatus(Status $status) {...}<\/code><\/pre>\n\n\n\n2. Fibers<\/h3>\n\n\n\n This PHP 8.1 update, allows to manage the possible interruptions in the flow of your code, this means that it controls the versatility of the page<\/strong>, it allows that if several people work simultaneously in the programming of a page, these can carry out their work without being lost or damaged, creating a multiwork system<\/strong>.<\/p>\n\n\n\nWith the upgrade, it goes from this:<\/p>\n\n\n\n
$httpClient->request('https:\/\/example.com\/')\n ->then(function (Response $response) {\n return $response->getBody()->buffer();\n })\n ->then(function (string $responseBody) {\n print json_decode($responseBody)['code'];\n });<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
$response = $httpClient->request('https:\/\/example.com\/');\nprint json_decode($response->getBody()->buffer())['code'];<\/code><\/pre>\n\n\n\n3. Never return type<\/h3>\n\n\n\n The Never<\/strong> return type indicates that a function is terminated and does not return any other value, for example, if in the function “pen” you enter “blue”, “red”, and “black”, none of the three can be found in any function other than “pen”.<\/p>\n\n\n\nWith the PHP 8.1 update, this is no longer the case:<\/p>\n\n\n\n
function redirect(string $uri) {\n header('Location: ' . $uri);\n exit();\n}\n \nfunction redirectToLoginPage() {\n redirect('\/login');\n echo 'Hello'; \/\/ <- dead code\n}<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
function redirect(string $uri): never {\n header('Location: ' . $uri);\n exit();\n}\n \nfunction redirectToLoginPage(): never {\n redirect('\/login');\n echo 'Hello'; \/\/ <- dead code detected by static analysis \n}<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n<\/div>\n\n\n\n
4. New expression in constructor<\/h3>\n\n\n\n This feature of PHP 8.1, allows using objects as default parameter values, static variables and global constants, as well as in attribute arguments. This means that it helps web management, making it more organized<\/strong>.<\/p>\n\n\n\nWith the update, it goes beyond this:<\/p>\n\n\n\n
{ class Service \n{\n private Logger $logger;\n \n public function __construct(\n ?Logger $logger = null,\n ) {\n $this->logger = $logger ?? new NullLogger();\n }\n}<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
class Service \n{\n private Logger $logger;\n \n public function __construct(\n Logger $logger = new NullLogger(),\n ) {\n $this->logger = $logger;\n }\n}<\/code><\/pre>\n\n\n\n5. Read-only properties<\/h3>\n\n\n\n With this new update, you cannot edit the content once the read-only mode is started, i.e. the content is kept intact until the read-only mode is finished<\/strong>.<\/p>\n\n\n\nWith the PHP 8.1 update, this is no longer the case:<\/p>\n\n\n\n
{ class BlogData\n{\n private Status $status;\n \n public function __construct(Status $status) \n {\n $this->status = $status;\n }\n \n public function getStatus(): Status \n {\n return $this->status; \n }\n}<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
class Demo {\n \n public string $Mutable;\n \n public readonly string $Immutable;\n \n public function __construct(\n string $Mutable,\n string $Immutable) {\n \n $this -> Mutable = $Mutable;\n $this -> Immutable = $Immutable;\n }\n \n}<\/code><\/pre>\n\n\n\n6. First-class Callable Syntax<\/h3>\n\n\n\n It serves to replace existing encodings that use strings and arrays. This means that it simplifies the programmers’ language to make it much simpler<\/strong>.<\/p>\n\n\n\nWith the update, it goes from this:<\/p>\n\n\n\n
$foo = [$this, 'foo'];\n\n$fn = Closure::fromCallable('strlen');<\/code><\/pre>\n\n\n\nTo this:<\/p>\n\n\n\n
$foo = $this->foo(...);\n\n$fn = strlen(...);<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n