What's new in PHP land? 2022.2

@brisphp1

https://brisphp.com

#BrisPHP

@brisphp1

https://brisphp.com

#BrisPHP

PHP Version Stats

@brisphp1

https://brisphp.com

#BrisPHP

PHP Version Stats

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: SensitiveParameter Attribute

  • Stacktraces show parameter values
  • Sometimes these are sensitive (eg. passwords)

@brisphp1

https://brisphp.com

#BrisPHP

function passwordHash(string $password) {
    var_dump(debug_backtrace());
}

passwordHash('hunter2');

PHP 8.2: SensitiveParameter Attribute

array(1) {
  [0]=>
  array(4) {
    ["file"]=> string(38) "..."
    ["line"]=> int(9)
    ["function"]=> string(3) "passwordHash"
    ["args"]=> array(1) {
      [0]=> string(38) "hunter2"
    }
  }
}

@brisphp1

https://brisphp.com

#BrisPHP

function passwordHash(#[SensitiveParameter] string $password) {
    var_dump(debug_backtrace());
}

passwordHash('hunter2');

PHP 8.2: SensitiveParameter Attribute

array(1) {
  [0]=>
  array(4) {
    ["file"]=> string(38) "..."
    ["line"]=> int(9)
    ["function"]=> string(3) "passwordHash"
    ["args"]=> array(1) {
     [0]=> object(SensitiveParameterValue)#1 (0) {
     }    
    }
  }
}

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: Regex "no capture"

preg_match returns:

  1. The entire match
  2. All capture groups
  3. All named capture groups

 

Note: Named capture groups are actually returned twice.

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: Regex "no capture"

preg_match('/(\w)-(?P<num>\d+)-(\w)/', 'a-123-b', $matches);

array(5) {
  [0]=> string(7) "a-123-b"
  [1]=> string(1) "a"
  ["num"]=> string(3) "123"
  [2]=> string(3) "123"
  [3]=> string(1) "b"
}

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: Regex "no capture"

preg_match with `/n` modifier returns:

  1. The entire match
  2. All named capture groups

 

Note: Named capture groups are still  returned twice.

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: Regex "no capture"

preg_match('/(\w)-(?P<num>\d+)-(\w)/n', 'a-123-b', $matches);

array(3) {
  [0]=> string(7) "a-123-b"
  ["num"]=> string(3) "123"
  [1]=> string(3) "123"
}
preg_match('/(?:\w)-(?P<num>\d+)-(?:\w)/', 'a-123-b', $matches);

Equivalent to non-named groups being non-capturing groups

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: memory_reset_peak_usage

$string = str_repeat('a', 1024 * 1024 * 4);
memory_get_peak_usage(); // 4590752

unset($string);

$string = str_repeat('a', 1024 * 1024 * 3);
memory_get_peak_usage(); // 4590752

Peak memory usage is the max memory used in the entire run

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: memory_reset_peak_usage

 $string = str_repeat('a', 1024 * 1024 * 4);

 memory_get_peak_usage(); // 4590752

 unset($string);

 memory_reset_peak_usage();

 $string = str_repeat('a', 1024 * 1024 * 3);

 memory_get_peak_usage(); // 3542648

Reset to record different peaks across different invocations

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: Readonly classes

class MyValueObject {
    public readonly string $myValue;
}

readonly class MyValueObject {
    public string $myValue;
}
  • Makes all properties readonly
  • Dynamic properties forbidden
  • All properties must be typed
  • Cannot opt-out of readonly
  • Children are read-only as well

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: true type

class Foo {

    private true $processed;

    public function process(string|int|true $value): true {
    }
}

function foo(true|bool $value) {}
// Fatal error: Duplicate type true is redundant

function foo(true|false $value) {}
// Fatal error: Duplicate type true is redundant

function foo(true&string $value) {}
// Fatal error: Type true cannot be part of an intersection type
  • Opposite of `false` from 8.0
  • Cannot union with `false` or `bool`
  • Cannot intersect with anything

@brisphp1

https://brisphp.com

#BrisPHP

PHP 8.2: utf8_encode/utf8_decode

Deprecated because:

  • Actually convert between Latin 1 and UTF-8
  • Does not care what the original encoding is at all
 // Exactly what utf8_encode does
 mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1');
 
 // Try to guess the correct character encoding first
 mb_convert_encoding($string, 'UTF-8', mb_list_encodings());

Questions?

@brisphp1

https://brisphp.com

#BrisPHP

Made with Slides.com