I am Hack Sparrow
Captain of the Internets.

Check object type in PHP

How to check the type of an object in PHP

Been working on something where you need to check the type of PHP objects? Doing that is pretty straightforward. In fact, there are two ways to do it, I'll show you both.

When you want to test an object for a particular type

You know there are predefined object types in PHP, you just call the is_* functions to test. The following code shows you how to do the _is* testing.

  1. is_null($var) - true if $var is null
  2. is_object($var) - true if $var is an object
  3. is_string($var) - true if $var is a string
  4. is_numeric($var) - true if $var is a numeric string
  5. is_int($var) - true if $var is an integer
  6. is_float($var) - true if $var is floating point number
  7. is_bool($var) - true if $var is a boolean
  8. is_array($var) - true if $var is an array
  9. is_resource($var) - true if $var is a resource (mysql, file etc). Use get_resource_type to get the specific type.
  10. is_scalar($var) - true if $var is a scalar (primitive data types: int, float, string, boolean)
  11. is_callable($var) - true if $var is a function or works like a function

So what's the difference between is_numeric and is_int? is_numeric will validate true even for quoted numbers (technically strings), eg: '31337', along with true numbers, eg: 31337. All non-binary form data come as strings - even numbers. In http://example.com/items?id=23434, $_GET[id] is a string! To check if the value of id is actually a number, you would need to use is_numeric instead of is_int. is_int will return true for 31337, but not for '31337'. The scope of using is_int is only within the PHP file, because all other numbers from external sources are passed to PHP as strings.

There are other is_* functions like is_integer, is_real, is_double etc. They are just aliases to one of the functions listed above. I have no idea why on earth they decided to have those aliases. I have always maintained that PHP is a very badly designed programming language. You might want to check out Node.js, if you are familiar with JavaScript.

When you want to get the type of the object

Sometimes, you don't want to test if the object is of any particular type - you just want to know its type - whatever it might be. Doing that is pretty easy - gettype().

Calling gettype on an object will return these possible values:

  1. "boolean"
  2. "integer"
  3. "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
  4. "string"
  5. "array"
  6. "object"
  7. "resource"
  8. "NULL"
  9. "unknown type"

There is a downside to using gettype: the return value may change in the future - according to the official PHP documentation. What it means is that your code might work fine for now, but once you upgrade PHP or your web host does it for you, your code is likely to stop working. No guarantee. Isn't that scary?

In this article, you have learnt how to check if an object is of a particular type, and to get the type of an object in PHP. If you were observant enough, you might also have learnt that PHP is a very badly designed programming language. If you are looking for an alternative to PHP, check out Node.js, it is a serverside development platform in JavaScript. It is going to change the world.

Make a Comment