-
Notifications
You must be signed in to change notification settings - Fork 22
/
ReflectionHelpers.inc
74 lines (67 loc) · 1.38 KB
/
ReflectionHelpers.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @file
*
* A collection of functions that help with reflection like properties.
*
*/
/**
* Checks to see if the given variable has a class.
*
* @param mixed $var
* @return boolean
*/
function has_class($var) {
return isset($var) && is_object($var);
}
/**
* Check to is if the object matches the given type.
*
* @param object $obj
* @param string $type
*
* @return boolean
*/
function is_type($obj, $type) {
return has_class($obj) && get_class($obj) == $type;
}
/**
* Checks if the object is a decendant of the provided type.
*
* @param object $child
* @param string $type
*
* @return boolean
*/
function is_descendant($obj, $type) {
$class = has_class($obj) ? get_class($obj) : NULL;
if ($class) {
$reflection = new ReflectionClass($class);
return $reflection->isSubclassOf($type);
}
return FALSE;
}
/**
* Checks to see if the object matches the given type, or if the object descends from the given type.
*
* @param object $obj
* @param string $type
*
* @return boolean
*/
function is_or_descends_from($obj, $type) {
return is_type($obj, $type) || is_descendant($obj, $type);
}
/**
*
* @param mixed $obj
* @param string $interface
* @return boolean
*/
function has_interface($obj, $interface) {
if (has_class($obj)) {
$implements = class_implements(get_class($obj));
return isset($implements[$interface]);
}
return FALSE;
}