Posts about PHP (old posts, page 2)
-
PHP 5: if (\false)
I was wondering why the typical class alias boilerplate code uses this weird backslash in
if (\false)
like here for example.It turns out this code will wish you a happy debugging in PHP 5.3-5.6:
Not a very useful knowledge now but an interesting history fact.
-
PHP 8.1 Syntax Showcase
PHP 8.1 was released when I was suffering from COVID so I'm a bit late.
<?php enum Status { case OK; case Error; } #[SomeAttr(new SomeNestedAttr())] class TestTest { public function __construct( public readonly Status $status = Status::OK, private Logger $logger = new FileLogger(perm: 0o644), ) {} public function process(Iterator&Countable $value) { if (count($value) === 0) { $this->error(); } } public function error(): never { $this->logger->error('Error!'); exit; } } $test = new TestTest(); $arr1 = ['a' => new ArrayObject()]; $arr2 = ['b' => new ArrayIterator()]; $result = array_map($test->process(...), [...$arr1, ...$arr2]);
-
Torrent File 2.2
-
CVE-2021-41106
Nice, now I have my first CVE :D The one I've found, not the one I've created :D
If you're using
lcobucci/jwt
, please upgrade. -
IsResource
Inspired by a recent discussion in the PHP Internals, I decided to provide a userland solution. Check this:
<?php $hash = hash_init('md5'); // vanilla functions: is_resource($hash); // true in PHP <= 7.1, false in PHP >= 7.2 get_resource_type($hash); // "Hash Context" in PHP <= 7.1, various errors otherwise // library functions: \Arokettu\IsResource\is_resource($hash); // true \Arokettu\IsResource\get_resource_type($hash); // "Hash Context"
Get it on my dev portal: https://sandfox.dev/php/is-resource.html
-
Pseudolocale Lib for PHP
I haven't found a good standalone pseudolocalization library for PHP so I wrote my own.
https://sandfox.dev/php/pseudolocale.html
Or if you're using Symfony Components or you don't mind using huge libraries for simple tasks, there is one good alternative:
PseudoLocalizationTranslator
in the Symfony Translation Component since version 5.2. -
PHP 8.0 Comparison Change
Another new thing in PHP 8.0 will be slightly less madness in non strict value comparison.
-
OOP in PHP 4 and 8
Deprecation of class name constructors means that you no longer can write OOP code that works both in PHP 4 and PHP 8 without some hackery. This works however:
<?php class WorksIn4And8 { var $int; // Regular method in PHP 4 // Constructor in 5-8 function __construct($int) { error_log('__construct()'); $this->int = $int; } // Constructor in PHP 4 // Constructor overridden by __construct in 5-7 // Regular method in PHP 8 function WorksIn4And8($int) { error_log('WorksIn4And8()'); $this->__construct($int); } function getInt() { return $this->int; } } $obj = new WorksIn4And8(48); var_dump($obj->getInt());
-
PHP 8.0 Syntax Showcase
Since RC1 is released today, I decided to make a small demonstration of PHP 8.0 syntax features.
-
Method Calls with Arbitrary Names
A discovery of the day. Many people know that you can define and read an object property with an arbitrary name: