Posts about PHP
-
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.
<?php #[FuncAttribute] function func(SplHeap|ArrayObject|null $object, mixed $value = null): ArrayAccess|false { return match ($object?->count()) { 0, null => throw new RuntimeException(), default => $object, }; } try { func(object: new ArrayObject()); } catch (RuntimeException) { // ignore }
-
Method Calls with Arbitrary Names
-
I released the first PHP 8 library ever
At least among the libraries listed on the Packagist. On this Tuesday sandfoxme/bencode was updated to 2.0 that requires PHP 8 as a minimum. While it was meant mostly as an experiment and a joke, it allowed me to try some new features like
Stringable
interface and remove some old compatibility handling thanks tombstring.func_overload
being finally removed. -
sabre/xml
An awesome discovery of today was made in an article titled An XML library for PHP you may not hate. As an unexpected twist I really didn't hate it, in fact it helped me to solve a problem that I had. It is called sabre/xml and it's a part of the sabre/dav project.
-
Install Composer in Docker
Here is a small snippet adapted from the official instruction:
RUN php -r "copy('https://composer.github.io/installer.sig', '/tmp/composer.sig');" && \ php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');" && \ php -r '$expected = file_get_contents("/tmp/composer.sig"); $actual = hash_file("sha384", "/tmp/composer-setup.php"); exit(intval(!hash_equals($expected, $actual)));' && \ php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer && \ chmod +x /usr/local/bin/composer && \ rm /tmp/composer-setup.php /tmp/composer.sig
It doesn't require a script file, doesn't deal with environment variables, and doesn't depend on the shell.
-
PHP 7.4: Splat Inconsistency
PHP 7.4 has this great new feature: splat operator now works in array expressions.
-
Encryptor
Created a simple cli file encryptor using libsodium
The app can create a password-protected file encoded with XSalsa20 and Argon2id
See my dev portal: https://sandfox.dev/php/encryptor.html