note 125367 added to language.oop5.anonymous
| From: | info at ensostudio dot ru | Date: | Wed, 16 Sep 2020 07:00:59 +0000 |
| Subject: | note 125367 added to language.oop5.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215119@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.167.36 (proxied: 31.129.215.166)
----
Manual Page -- http://php.net/manual/en/language.oop5.anonymous.php
Edit -- https://master.php.net/note/edit/125367
Del: integrated -- https://master.php.net/note/delete/125367/integrated
Del: useless -- https://master.php.net/note/delete/125367/useless
Del: bad code -- https://master.php.net/note/delete/125367/bad+code
Del: spam -- https://master.php.net/note/delete/125367/spam
Del: non-english -- https://master.php.net/note/delete/125367/non-english
Del: in docs -- https://master.php.net/note/delete/125367/in+docs
Del: other reasons-- https://master.php.net/note/delete/125367
Reject -- https://master.php.net/note/reject/125367
Search -- https://master.php.net/manage/user-notes.php