New features in PHP 8.1

New features in PHP 8.1

With the launching of Scriptcase 9.8 your projects can be deployed in an updated environment, with the PHP 8 or 8.1.
Kind reminder that it is still possible to make deployment in environments with outdated PHP, since it's compatible with PHP 5.6 or superior.
In order to avoid incompatibility problems with the code in your events or libraries, we recommend that the PHP used in your development environment be the same as the one used in production.

The update in the PHP brings numerous new improvements for Scriptcase, like performance and security improvement both in the development environment and in the applications generated by the tool.
Along with the PHP update, some functions are implemented or have their functioning changed in order to improve usability.

Bellow there's a list of some of the new features, to check the full list you can click here.

Readonly Properties

Support to read-only properties. This property type cannot be modified or edited after initialization.
How it worked before:
  1. Class BlogData
  2. {
  3.     private Status $status
  4.     public function __construct(Status $status)
  5.     {
  6.         $this->status = $status;
  7.     }
  8.     public function getStatus(): Status
  9.     {
  10.     return $this->status;
  11.     }
  12. }
Now in PHP 8.1:
  1. Class BlogData
  2. {
  3.     public readonly Statys $status
  4.     public function __construct(Status $status)
  5.     {
  6.         $this->status = $status;
  7.     }
  8. }

Operator "nullsafe" ?->

Implementing the nullsafe(?->) operator allows checking for null values in a much simpler way.
You can see below codes comparing to use of nullsafe operator in PHP 8 and how the same code would have been done in previous versions.

From PHP 8 on, this line:
  1. $result = $repository?->getUser(5)?->name;
Is the equivalent to the following code in previous PHP versions:
  1. if (is_null($repository)) {
  2.     $result = null;
  3. else {
  4.     $user = $repository->getUser(5);
  5.     if (is_null($user)) {
  6.         $result = null;
  7.     } else {
  8.         $result = $user->name;
  9.     }
  10. }

Typed Properties

Class properties now support type declarations.
  1. class User {
  2.     public int $id;
  3.     public string $name;
  4. }
The above example will enforce that $user->id can only be assigned int values and $user->name can only be assigned string values.

Union Types

Allows the same variable to accept two different types of data. To define the types the variable can receive, just use the pipe "|" between types.

Concised example of the use in code:
  1. function myFunction(int|float $number): int
  2. {
  3.     return round($number);
  4. }

Function str_contains

Allows the search inside an array returning a boolean answear.
For more information about this function access

You can read the RFC on this function in order to verify all its attributes.

  1. str_contains ( string $haystack , string $needle ) : bool
In the code above, the verification will be made to indicate if the string $needle is inside the string $haystack or not. If yes, it returns true, otherwise it returns false.

Functions str_starts_with() and str_ends_with()

These functions work similarly to the str_contains() however they check if a string starts or ends with a given string, saving CPU usage as it will not be necessary to traverse the entire string.

In addition to the similar behavior, the syntax is also similar to the str_contains function:
  1. str_starts_with (string $haystack , string $needle) : bool

  2. str_ends_with (string $haystack , string $needle) : bool

    • Related Articles

    • Incompatibility errors with after PHP 7.0

      The only errors that may occur are due to codes used in events, libraries, and methods that are not compatible with PHP 7.0. These errors must be corrected manually. 1 - Short open tags The short_open_tag directive tells PHP whether the abbreviated ...
    • Scriptcase 9.8 - Functions deprecated and/or alterated in PHP 8.1

      One of the features of the new version is the possibility to generate applications that are compatible with PHP 8 and 8.1. This way, users that are using Scriptcase 9.8 will be able to make deployments in environments with its PHP updated (versions 8 ...
    • Updating Scriptcase's PHP

      Currently, the installation of Scriptcase 9.x comes with version 7.0.14 of PHP, unfortunately, this version has a compatibility problem with the new version of authentication of MySQL 8.0, so it is necessary to perform the PHP update so the ...
    • Updating the environment from your Scriptcase 9 to PHP 7.3

      Starting from 9.4.016 release, ScriptCase will be able to work also with PHP 7.3.  Important note:         The PHP migration is not required. You can stil use older versions.         ScriptCase will be maintened for PHP 7.0, receving several updates. ...
    • Setting environment manually PHP 5.4 - CentOS

      This article describes how to install the PHP 5.4 on a CentOS 5 or CentOS 6 server 1. First, detect if any PHP packages are installed:      If packages are installed remove them, for example: 2. Add PHP 5.4 packages to yum using this command for ...
    • Popular Articles

    • Examples for diagrams

      This tutorial shows how you can import and use the new examples for diagrams released with Scriptpcase V9.2.16.  The applications were created using the components: Jsmind, JKanban e Orgchart.  - Sample 1: Mind Map diagram Click here to  execute the ...
    • Manual Update - Windows

      Before performing any procedure we recommend performing a backup of your projects. To back up Scriptcase: Go to Settings > Administration, in the side menu go to Services > Backup. A file (.ZIP) will be generated with all your projects, save this ...
    • PHP functions necessary for ScriptCase

      In PHP , there are some essential functions for the operation of Scriptcase .  On some servers , these functions are disabled , this makes the Scriptcase not work properly.  If you're using a shared hosting server, you'll need to contact the host ...
    • Oracle Connection - Scriptcase 9 - Linux 64 Bits

      This procedure will only work if you have a Scriptcase manual installation in your server. Installing PHP 7.0 Installing the Scriptcase Manually To connect with Oracle in a Linux System, we must follow these steps below: Step 1 -  Installing these ...
    • Using friendly URL

      The friendly URL was included from Scriptcase 7, in order to make the URL, of the applications generated by Scriptcase, cleaner and easier to associate them with applications. For example, using friendly URLs the link to a Scriptcase application ...