File: type.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood  >  Encase PHP Functional Programming  >  type.php  >  Download  
File: type.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Encase PHP Functional Programming
Make functions work as if they are class functions
Author: By
Last change:
Date: 1 year ago
Size: 944 bytes
 

Contents

Class file image Download
<?php
namespace Encase\Functional;

/**
 * Get the type of a variable.
 * Based on which of PHP's is_* checks returns true rather than using gettype.
 * Returns "function" for closure objects but does not work with other
 * callables as those are strings and arrays first.
 *
 * @param mixed $value
 * @return string|null One of: "array", "bool", "int", "float", "function",
 * "null", "object", "resource", "string"
 */
function type($value): string
{
    if (\
is_array($value)) {
        return
'array';
    } elseif (\
is_bool($value)) {
        return
'bool';
    } elseif (\
is_float($value)) {
        return
'float';
    } elseif (\
is_int($value)) {
        return
'int';
    } elseif (\
is_null($value)) {
        return
'null';
    } elseif (\
is_object($value)) {
        return
$value instanceof \Closure ? 'function' : 'object';
    } elseif (\
is_resource($value)) {
        return
'resource';
    } elseif (\
is_string($value)) {
        return
'string';
    }
    return
'unknown type';
}


For more information send a message to info at phpclasses dot org.