PHP, how to use instanceof on an array

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?
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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
0
elepilAuthor 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.
0
elepilAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.