PHP object that can be implicitly cast to other types.
new \Chameleon(mixed $...);
mixed[] ...
keys used by array_key_exists
and foreach
(values are always instances of Chameleon).
Example:
<?php
$var = new \Chameleon;
Chameleon as a boolean
echo gettype(!!$var).PHP_EOL;
Chameleon as a string
echo gettype("$var").PHP_EOL;
Chameleon as an integer
echo gettype($var|0).PHP_EOL;
Chameleon as a float
echo gettype($var+.0).PHP_EOL;
Chameleon can be accessed like an array and has infinite elements of type Chameleon
echo get_class($var[0]).PHP_EOL;
Note: To ensure array_key_exists returns true for keys, pass them into the constructor or access them in some way.
$var = new Chameleon(0);
array_key_exists(0, $var); // true: passed in constructor
$var->a;
array_key_exists('a', $var); // true: key has been accessed
$var['b'] = null;
array_key_exists('b', $var); // true: key has been accessed
array_key_exists('c', $var); // false
Chameleon can be accessed like an multi-dimensional array and has infinite sub-elements of type Chameleon
get_class($var[0]['a']).PHP_EOL;
Chameleon is an object
gettype($var);
Chameleon has infinite properties of type Chameleon
get_class($var->a);
Chameleon has infinite methods with any number of parameters which return type Chameleon
get_class($var->abc(0, 'a'));
Chameleon has infinite static methods with any number of parameters which return type Chameleon
get_class(Chameleon::abc(0, 'a'))
Note: Any static method can be used in place of the constructor and passed keys via a special array argument.
<?php
$var = Chameleon::create(["Chameleon" => [0]]);
$var->a;
Chameleon is invokable with any number of parameters and returns type Chameleon
get_class($var(0, 'a'));
Chameleon is countable with a single element of key type integer and value of Chameleon by default
foreach ($var as $key => $value) {
echo gettype($key).' => '.get_class($value).PHP_EOL;
}
Note: To change the keys output in the foreach, pass them into the constructor or access them in some way.
$var = new Chameleon(0);
$var->a;
$var['b'] = null;
Chameleon can be cloned and returns a new instance of type Chameleon
$clone = clone $var;
echo get_class($clone).PHP_EOL;
For completeness Chameleon can be cast to null
echo gettype((unset) $var).PHP_EOL;