public static
string
|
#
name( mixed $X )
Returns the type name of $X
Returns the type name of $X
If $X is an object, returns the class name of the object.
Parameters
- $X
- A variable you want to know the type
Returns
string A type name or class name
Example
Demonstration
var_dump( type::name(true) );
var_dump( type::name(1) );
var_dump( type::name(1.0) );
echo "\n";
var_dump( type::name('string') );
var_dump( type::name(array ()) );
var_dump( type::name(tmpfile()) );
echo "\n";
var_dump( type::name(new stdClass()) );
string(7) "boolean"
string(7) "integer"
string(5) "float"
string(6) "string"
string(5) "array"
string(8) "resource"
string(8) "stdClass"
|
public static
boolean
|
#
matches( mixed $X, integer|string $Type )
Returns whether the type of $X matches a specific type
Returns whether the type of $X matches a specific type
Parameters
- $X
- A variable to check type
- $Type
- A type expression
Returns
boolean
Example
Demonstration
$var = '123';
var_dump( type::matches($var, 'boolean') );
var_dump( type::matches($var, 'bool') );
echo "\n";
var_dump( type::matches($var, 'integer') );
var_dump( type::matches($var, 'int') );
echo "\n";
var_dump( type::matches($var, 'string') );
var_dump( type::matches($var, 'str') );
echo "\n";
var_dump( type::matches($var, 'array') );
var_dump( type::matches($var, 'arr') );
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
Pseudo type matching
$var1 = '123';
var_dump( type::matches($var1, 'numeric') );
echo "\n";
$var2 = array (1, 2, 3);
var_dump( type::matches($var2, 'scalar') );
var_dump( type::matches($var2, 'vector') );
echo "\n";
$var3 = function () { };
var_dump( type::matches($var3, 'callable') );
bool(true)
bool(false)
bool(true)
bool(true)
Class matching
class Cat { }
class Dog { }
class Collie extends Dog { }
$dog = new Collie();
var_dump( type::matches($dog, 'Collie') );
var_dump( type::matches($dog, 'Dog') );
var_dump( type::matches($dog, 'Cat') );
bool(true)
bool(true)
bool(false)
|
public static
boolean
|
#
is_arr_like( mixed $X )
Returns whether $X is an array or array-like object
Returns whether $X is an array or array-like object
Parameters
- $X
- A variable to check type
Returns
boolean
Example
Demonstration
$var1 = array ();
$var2 = new ArrayObject();
$var3 = new stdClass();
var_dump( type::is_arr_like($var1) );
var_dump( type::is_arr_like($var2) );
var_dump( type::is_arr_like($var3) );
bool(true)
bool(true)
bool(false)
Using in if
$var = new ArrayObject();
if (type::is_arr_like($var)) {
$var[0] = 'A';
$var['X'] = 'B';
$var[] = 'C';
}
print_r( $var );
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => A
[X] => B
[1] => C
)
)
|
public static
boolean
|
#
is_iterable( mixed $X )
Returns whether $X is iterable
Returns whether $X is iterable
Parameters
- $X
- A variable to check type
Returns
boolean
Example
Demonstration
$var1 = array ();
$var2 = new ArrayObject();
$var3 = new stdClass();
var_dump( type::is_iterable($var1) );
var_dump( type::is_iterable($var2) );
var_dump( type::is_iterable($var3) );
bool(true)
bool(true)
bool(false)
Using in if
$var = new ArrayObject(array ('A', 'B', 'C'));
if (type::is_iterable($var)) {
foreach ($var as $i => $item) {
echo "$i: $item" . "\n";
}
}
0: A
1: B
2: C
|
public static
boolean
|
#
is_countable( mixed $X )
Returns whether $X is countable
Returns whether $X is countable
Parameters
- $X
- A variable to check type
Returns
boolean
Example
Demonstration
$var1 = array ();
$var2 = new ArrayObject();
$var3 = 'string';
var_dump( type::is_countable($var1) );
var_dump( type::is_countable($var2) );
var_dump( type::is_countable($var3) );
bool(true)
bool(true)
bool(false)
Using in if
$var = array ('A', 'B', 'C');
if (type::is_countable($var)) {
echo 'Number of elements: ' . count($var);
}
Number of elements: 3
|
public static
boolean
|
#
bool( mixed $X, boolean $Alt = false )
Evaluates $X as a boolean
Evaluates $X as a boolean
In detail:
- If
$X is an object, calls $X->toBoolean() or $X->toBool() if they exist.
- If
$X is a countable object, returns whether count($X) > 0 .
Parameters
- $X
- A variable to treat as a boolean
- $Alt
- (optional) An alternative value to return if evaluation has failed
Returns
boolean
Example
Number to boolean
var_dump( type::bool(1) );
var_dump( type::bool(0) );
var_dump( type::bool(-1) );
bool(true)
bool(false)
bool(true)
String to boolean
var_dump( type::bool('string') );
var_dump( type::bool('') );
bool(true)
bool(false)
Array to boolean
var_dump( type::bool(array ('A', 'B', 'C')) );
var_dump( type::bool(array ()) );
bool(true)
bool(false)
Semantic evaluation
var_dump( type::bool('false') );
var_dump( type::bool('False') );
var_dump( type::bool('FALSE') );
var_dump( type::bool('null') );
var_dump( type::bool('Null') );
var_dump( type::bool('NULL') );
var_dump( type::bool('no') );
var_dump( type::bool('No') );
var_dump( type::bool('NO') );
var_dump( type::bool('off') );
var_dump( type::bool('Off') );
var_dump( type::bool('OFF') );
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
Methods evaluation
class Truthy {
function toBool() {
return true;
}
}
class Falsy {
function toBool() {
return false;
}
}
$obj1 = new Truthy();
$obj2 = new Falsy();
var_dump( type::bool($obj1) );
var_dump( type::bool($obj2) );
bool(true)
bool(false)
|
public static
integer
|
#
int( mixed $X, integer $Alt = 0 )
Evaluates $X as an integer
Evaluates $X as an integer
If $X is an object, calls $X->toInteger() or $X->toInt() if they exist.
Parameters
- $X
- $Alt
- (optional) An alternative value to return if evaluation has failed
Returns
integer
Example
Demonstration
var_dump( type::int(true) );
var_dump( type::int(false) );
echo "\n";
var_dump( type::int('string') );
var_dump( type::int('0123') );
echo "\n";
var_dump( type::int(array ('A', 'B', 'C')) );
var_dump( type::int(array ()) );
int(1)
int(0)
int(0)
int(123)
int(1)
int(0)
Methods evaluation
class Hundred {
function toInt() {
return 100;
}
}
$obj = new Hundred();
var_dump( type::int($obj) );
var_dump( type::int($obj) * 3 );
int(100)
int(300)
Fail-safe
$obj = new stdClass();
var_dump( type::int($obj) );
var_dump( type::int($obj, -1) );
int(0)
int(-1)
|
public static
string
|
#
str( mixed $X, string $Alt = '' )
Evaluates $X as an string
Evaluates $X as an string
If $X is an object, calls $X->__toString() , $X->toString() or $X->toStr() if they exist.
Parameters
- $X
- $Alt
- (optional) An alternative value to return if the evaluation failed
Returns
string
Example
Demonstration
var_dump( type::str(true) );
var_dump( type::str(false) );
echo "\n";
var_dump( type::str(1) );
var_dump( type::str(0) );
var_dump( type::str(-1) );
echo "\n";
var_dump( type::str(1.23) );
string(1) "1"
string(0) ""
string(1) "1"
string(1) "0"
string(2) "-1"
string(4) "1.23"
Semantic evaluation
$var1 = array ('A', 'B', 'C');
$var2 = array (
'X' => 'A',
'Y' => 'B',
'Z' => 'C'
);
var_dump( type::str($var1) );
var_dump( type::str($var2) );
string(7) "A, B, C"
string(16) "X: A, Y: B, Z: C"
Method evaluation
class Greeting {
function toStr() {
return 'Hello';
}
}
$obj = new Greeting();
var_dump( type::str($obj) );
echo type::str($obj).' World.';
string(5) "Hello"
Hello World.
|
public static
array
|
#
arr( mixed $X, array $Alt = array () )
Evaluates $X as an array
If $X is an object, calls $X->toArray() or $X->toArr() if they exist.
Parameters
- $X
- $Alt
- (optional) An alternative value to return if casting failed
Returns
array
Example
Demonstration
var_dump( type::arr(null) );
var_dump( type::arr(true) );
var_dump( type::arr(1) );
array(0) {
}
array(1) {
[0]=>
bool(true)
}
array(1) {
[0]=>
int(1)
}
Method evaluation
class Stack {
private $items;
function __construct() {
$this->items = func_get_args();
}
function toArray() {
return $this->items;
}
}
$obj = new Stack('A', 'B', 'C');
var_export( type::arr($obj) );
array (
0 => 'A',
1 => 'B',
2 => 'C',
)
|