1. Go to this page and download the library: Download samuelludwig/phprelude library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1); namespace StructTest;
array like
$a = [ 'pets_name' => 'mark', 'pet_kind' => 'dog' ];
// and I have some function thats expecting the keys to be named differently, like
// "`animal_name`" instead of "`pet_name`" and "`species`" instead of "`pet_kind`",
// and maybe its also expecting the `species` to always be capitalized, I could
// then write
$new_a =
Enum\extract_values_into_format(
$a,
[ 'animal_name' => 'pets_name'
, 'species' => ['pet_kind', fn($x) => strtoupper($x)]
]);
// Et voila, we have our new array
echo $new_a == [ 'animal_name' => 'mark', 'species' => 'DOG' ]; // 1
declare(strict_types=1); namespace StructTest;
'first' => 'door', 'second' => 'cat', 'third' => 'orange' ];
// We can extract specific values into a list via:
[ $x, $y ] = Enum\extract_values($a, ['third', 'first']);
echo "$x $y"; // "orange door"
declare(strict_types=1); namespace StructTest;
e can use const to pseudo-namespace our struct without too much hassle
const UserT = __NAMESPACE__ . '\User';
p\defstruct(
UserT,
[ 'id' => [['int', 'string'], 0]
, 'name' => [['string']]
]);
$user0 = p\mk(UserT, ['name' => 'John']);
$user1 = p\mk(UserT, ['id' => 1, 'name' => 'Mike']);
echo Json\encode($user0) . "\n"; // {"id":0,"name":"John"}
echo Json\encode($user1) . "\n"; // {"id":1,"name":"Mike"}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.