What's new in PHP land? 2023.1
@brisphp1
https://brisphp.com
#BrisPHP
Giveaway: brisphp.com/pizza
@brisphp1
https://brisphp.com
#BrisPHP
✅ PHP 8.2 Released
✅ PHP 8.1 Active until Nov
⚠️ PHP 8.0 Security only
❌ PHP 7.4 EOL
@brisphp1
https://brisphp.com
#BrisPHP
PHP Version stats
@brisphp1
https://brisphp.com
#BrisPHP
PHP Version stats
@brisphp1
https://brisphp.com
#BrisPHP
PHP Foundation 2022 Wrap
- Raised $580,000 from 1400 donors
- Received 100 dev resumes
- Hired 6 part time devs
- To hire one more this year
- To improve transparency this year
- Devs to increase hours worked (4 FTEs)
@brisphp1
https://brisphp.com
#BrisPHP
PHP Foundation 2022 Wrap
Commits
Reviews
@brisphp1
https://brisphp.com
#BrisPHP
trait FooTrait {
public function doFoo(int $value): void {
if ($value > self::MAX_VALUE) {
throw new \Exception('out of range');
}
}
}
class FooClass {
private const MAX_VALUE = 42;
use FooTrait;
}
PHP 8.2 Constants in traits
@brisphp1
https://brisphp.com
#BrisPHP
class User {
const HASH = 'bcrypt';
public function __construct(
public $firstName,
public $lastName,
) {}
public function intro()
{
return "My name is {$this->firstName} {$this->lastName}";
}
}
PHP 8.3 Dynamic class constants & Enums
@brisphp1
https://brisphp.com
#BrisPHP
$dan = new User('Dan', 'Murrphy');
// Dynamic propertey
$prop = 'firstName';
echo $dan->$prop;
// Dynamic variable
$var = 'dan';
echo $$var->firstName;
echo $$var->$prop;
// Dynamic method
$method = 'intro';
echo $dan->{$method}();
echo $$var->{$method}();
PHP 8.3 Dynamic class constants & Enums
@brisphp1
https://brisphp.com
#BrisPHP
$dan = new User('Dan', 'Murrphy');
$constant = 'HASH';
$class = 'User';
echo User::HASH;
echo $class::HASH;
// PHP < 8.3
echo \constant("User::{$constant}");
// PHP 8.3+
echo User::{$constant};
echo $class::{$constant};
PHP 8.3 Dynamic class constants & Enums
@brisphp1
https://brisphp.com
#BrisPHP
enum Hash: string {
case Bcrypt = 'bcrypt';
case Sha256 = 'sha256';
}
$case = 'Sha256';
$enum = 'Hash';
echo Hash::Sha256->value;
echo $enum::Sha256->value;
// PHP < 8.3
echo \constant("Hash::{$case}")->value;
// PHP 8.3+
echo Hash::{$case};
echo $enum::{$case};
PHP 8.3 Dynamic class constants & Enums
@brisphp1
https://brisphp.com
#BrisPHP
unserialize('foo');
// Notice: unserialize(): Error at offset 0 of 3 bytes
unserialize('i:12345678901234567890;');
// Warning: unserialize(): Numerical result out of range
unserialize('E:3:"foo";');
// Warning: unserialize(): Invalid enum name 'foo' (missing colon)
// Notice: unserialize(): Error at offset 0 of 10 bytes
unserialize('E:3:"fo:";');
// Warning: unserialize(): Class 'fo' not found
// Notice: unserialize(): Error at offset 0 of 10 bytes
PHP 8.3 unserialize error handling
@brisphp1
https://brisphp.com
#BrisPHP
try {
unserialize('O:19:"SplDoublyLinkedList":3:{i:0;i:0;i:1;N;i:2;a:0:{}}');
} catch (UnexpectedValueException $e) {
echo $e->getMessage();
// Incomplete or ill-typed serialization data
}
try {
unserialize(
'a:2:{i:12345678901234567890;O:19:"SplDoublyLinkedList":3:{i:0;i:0;i:1;N;i:2;a:0:{}}}'
);
// Warning: unserialize(): Numerical result out of range
// Notice: unserialize(): Unexpected end of serialized data
// Notice: unserialize(): Error at offset 83 of 84 bytes
} catch (UnexpectedValueException $e) {
echo $e->getMessage();
// Incomplete or ill-typed serialization data
}
PHP 8.3 unserialize error handling
@brisphp1
https://brisphp.com
#BrisPHP
try {
set_error_handler(static function ($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
$result = unserialize($serialized);
} catch (\Throwable $e) {
// Unserialization failed.
// Catch block optional if the error should not be handled.
} finally {
restore_error_handler();
}
// Do something with the $result. Must not appear
// in the 'try' to not 'catch' unrelated errors.
var_dump($result);
PHP 8.3 unserialize error handling
@brisphp1
https://brisphp.com
#BrisPHP
try {
$result = unserialize($serialized);
// Do something with the $result
// Can appear in the 'try'
var_dump($result);
} catch (\UnserializationFailureException $e) {
// unserialization failed.
var_dump($e->getPrevious());
}
PHP 8.3 unserialize error handling
BC Breaks
- Exception name clash
- Changed exception handling
Questions?
@brisphp1
https://brisphp.com
#BrisPHP
Observability Panel
- Justin Hennessy
- Ben Plunkett
- Damian Maclennan
What's new in PHP land - 2023.1
By Nathan Dench
What's new in PHP land - 2023.1
- 143