<?php
final class ArrayTools
{
public static function get_property(array $fields, string $field, string $type)
{
if (array_key_exists($field, $fields) === false)
{
throw new Exception('field');
}
$value = $fields[$field];
if (gettype($value) !== $type)
{
throw new Exception(gettype($value) . ' === ' . $type);
}
return $value;
}
}
(new class
{
public function check_fields(array $fields): void
{
echo ArrayTools::get_property($fields, 'lens', 'integer');
echo ArrayTools::get_property($fields, 'name', 'string');
# === property exception ===
# echo ArrayTools::get_property($fields, 'null', 'NULL');
# === datatype exception ===
# echo ArrayTools::get_property($fields, 'time', 'NULL');
}
public function __construct()
{
$this->check_fields(['name' => 'anna', 'lens' => 17, 'time' => 100]);
}
});
To embed this project on your website, copy the following code and paste it into your website's HTML: