PHP 8.3: le novità della nuova versione

Introduzione

Le versioni Minor di PHP, come PHP 8.3, introducono miglioramenti e funzionalità.

1. Aggiunta dei Typed Class Constants

PHP 8.3 introduce i Typed Class Constants, permettendo ai programmatori di dichiarare costanti di classe tipizzate.


class Example {
    public const string NAME = 'Esempio';
}



interface I {
    const string PHP = 'PHP 8.1';
}

class Bar83 implements I {
    const string PHP = 'PHP 8.3';
}
echo 'Bar83::PHP: ' . Bar83::PHP; //print 'PHP 8.3'

class FooPHPArr implements I {
    const string PHP = []; // Fatal error: Cannot use array as value for class constant FooPHPArr::PHP of type string
}

2. Attributo #[\Override]

Con l’aggiunta dell’attributo #[\Override] a un metodo, PHP si assicura che esista un metodo con lo stesso nome in una classe genitore o in un’interfaccia implementata. Questa funzionalità rende sia piu leggibile il codice che semplificato il refactoring in quanto verrà rilevata la rimozione o il rename di un metodo sovrascritto.


//classe padre
class TestCase {

    protected function setUp(): void {
    }

    protected function tearDown(): void {
    }
}

/*

PHP < 8.3

final class MyTest extends TestCase {
    protected $logFile;

    protected function setUp(): void {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }

    protected function taerDown(): void {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

// The log file will never be removed, because the
// method name was mistyped (taerDown vs tearDown).

*/

//PHP 8.3

final class MyTest extends TestCase {
    protected $logFile;

    #[\Override]
    protected function setUp(): void {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }

    #[\Override]
    //protected function taerDown(): void {
    protected function tearDown(): void {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}
// PHP Fatal error:  MyTest::taerDown() has #[\Override] attribute, but no matching parent method exists

3. funzione json_validate()

la funzione json_validate() verifica se una stringa è un JSON valido.



var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true


4. nuovi metodi di Randomizer

La Random Extension introdotta nelle versioni precedentei, con PHP 8.3 viene arricchita di nuovi metodi.

Il metodo getBytesFromString restituisce una sringa causale di caratteri estratti dalla stringa di caratteri passata come argomento.


$randomizer = new \Random\Randomizer();
printf(
    "%s.example.com",
    $randomizer->getBytesFromString('abcdefghijklmnopqrstuvwxyz0123456789', 16)
);
output something similar to:
3zsw04eiubcf82jd.example.com

Il metodo getFloat(float $min, float $max, Random\IntervalBoundary $boundary = Random\IntervalBoundary::ClosedOpen): float
restituisce un numero float casuale, quanto piu possibile uniformemente distribuito dall intervallo ($min, $max).


Generate a random latitude and longitude:
$randomizer = new \Random\Randomizer();
// Note that the latitude granularity is double the
// longitude's granularity.
//
// For the latitude the value may be both -90 and 90.
// For the longitude the value may be 180, but not -180, because
// -180 and 180 refer to the same longitude. ???
var_dump(sprintf(
    "Lat: %+.6f Lng: %+.6f",
    $randomizer->getFloat(-90, 90, \Random\IntervalBoundary::ClosedClosed),
    $randomizer->getFloat(-180, 180, \Random\IntervalBoundary::OpenClosed),
)); // string(32) "Lat: -51.742529 Lng: +135.396328"



Il metodo nextFloat(): float
restituisce un numero float casuale, quanto piu possibile uniformemente distribuito dall’intervallo semiaperto [0, 1).


$r = new \Random\Randomizer();
print $r->nextFloat();

Riferimenti Utili

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *