note 125367 deleted from language.oop5.anonymous by cmb
| From: | cmb@php.net | Date: | Wed, 16 Sep 2020 07:08:18 +0000 |
| Subject: | note 125367 deleted from language.oop5.anonymous by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215120@lists.php.net to get a copy of this message | ||
Note Submitter: info at ensostudio dot ru
----
Note: format/pattern of the class name may be other, but it's always(tested in 7.x) has
"class@anonymous" prefix.
Validation function:
<?php
/**
* Whether given value is instance or name of anonymous class.
*
* @param object|string $value An object instance or a class name
* @return bool
* @throws InvalidArgumentException Invalid value
* @author info@ensostudio.ru
*/
function isAnonymousClass($value): bool
{
if (is_object($value)) {
$value = get_class($value);
} elseif (! is_string($value)) {
throw new InvalidArgumentException('Value is not an object or string');
} elseif (! class_exists($value)) {
throw new InvalidArgumentException("Class {$value} not exists");
}
// only anonymous class can use @ in name
return strpos($value, '@') !== false;
}
?>
+ common version:
<?php
/**
* Whether given value is instance/name of anonymous class/function.
*
* @param object|string $value An object instance or a class name
* @return bool
* @throws InvalidArgumentException Invalid value
* @author info@ensostudio.ru
*/
function isAnonymous($value): bool
{
if (is_object($value)) {
$value = get_class($value);
} elseif (! is_string($value)) {
throw new InvalidArgumentException('Value is not an object or string');
} elseif (! class_exists($value)) {
throw new InvalidArgumentException("Class {$value} not exists");
}
// only anonymous class can use @ symbol in name
return in_array($value, ['stdClass', 'Closure'], true)
|| strpos($value, '@') !== false;
}
?>