PHP, how to use instanceof on an array

elepil
elepil used Ask the Experts™
on
I did a var_dump on a variable called $row, and I got this:

array (size=1)
  'name' => string 'Glenn' (length=5)

Open in new window


So I know it is an array. But when I do this:

echo $row instanceof array;

Open in new window


I get a syntax error: Parse error: syntax error, unexpected 'array' (T_ARRAY).

Now I know there is the is_array, but is there no way I can use instanceof on an array?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
Apparently not.  'instanceof' is used with objects and classes to determine whether they are members of a specific object or class.  'array' is neither, it is a 'type' in PHP.

http://php.net/manual/en/internals2.opcodes.instanceof.php

http://php.net/manual/en/language.operators.type.php

http://php.net/manual/en/language.types.array.php
Most Valuable Expert 2011
Top Expert 2016
Commented:
There is a lot of terminology (some might say jargon) in object-oriented programming.  In OOP parlance, an object in instantiated from a class.  The class is the blueprint; the object is the working thing.  If I make three objects from the same class definition, I have created three instances of the class.  Each of the objects will satisfy the instanceof operator.
http://php.net/manual/en/language.operators.type.php
http://php.net/manual/en/language.operators.precedence.php

Using instanceof is kind of like guessing where your object came from.  Using get_class() is kind of like asking where your object came from.  I have never found a practical use for these in well-organized object oriented programs, where a common design pattern is "tell, don't ask."

Related:
http://php.net/manual/en/function.is-a.php (a checkered history)
http://php.net/manual/en/function.get-class.php

Some of the broader theory:
http://www.experts-exchange.com/articles/18329/SOLID-Design-in-PHP-Applications.html

Author

Commented:
It was Dave's answer that led me to the answer when he said 'array' is a type.

Both of you have helped me a lot to date, and I wonder if the days are gone when I'd ask a question and get a very specific answer. It seems I've been getting a slew of links that point to generalities lately, especially from Ray.

Both your responses did help though, but I had to spend a bit more time going through your links. I now know I have to use gettype() first, and if it says object, then I can use getclass() to determine what class it is. gettype() would've also returned 'array' if the variable were an array, which was what I was looking for. Had responses run along these lines, I would've given an 'A' for the solution.

Thank you both for your help.

Author

Commented:
The definition of 'A' grade says "or is a link to information that answered the question". While that is technically correct, you really do take advantage of that as you always give me links that go way beyond the scope of my question. But just because the needle resides in one of the haystacks does not necessarily make a good answer.

I frown upon your practice of changing the grade; this would be the third time you've done this to me. I will have to talk to EE's customer service personnel about this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial