1. Go to this page and download the library: Download fasano/phprimitives 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/ */
fasano / phprimitives example snippets
readonly class Email extends AbstractString
{
protected static function validate(string $value): void
{
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address: ' . $value);
}
}
}
# Input boundary - scalar to primitive
$age = new Age($request->input('age')); # Throws if invalid
$email = new Email($request->input('email')); # Throws if invalid
# Everywhere else - work with primitives
function createUser(Email $email, Age $age): User
{
# Type system guarantees correctness
# No defensive checks
function createUser(string $email, int $age): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { /* ... */ }
if ($age < 0 || 120 < $age) { /* ... */ }
# Repeat in every function that touches these values
}
echo json_encode(new Person(
new Name('John'),
new Age(18),
new Email('[email protected]'),
));
// {"name":"John","age":18,"email":"[email protected]"}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.