468,256 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

typeof in PHP?

Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do
this:

if($obj typeof Iterator)
...

or

if(typeof($obj) == Iterator)
...
It seems really kludgy to just try to iterate over something and wait
for it to fail, but AFAIK that's the only way to do this in PHP.

Can someone please point me in the right direction on this? Many thanks
in advance.

Cheers,
Joshua Frank
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 20 '06 #1
7 42591
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archimetrics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

Apr 20 '06 #2
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.

Thanks again.

Wayne wrote:
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archimetrics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 20 '06 #3
You may also find the following helpful:

http://us3.php.net/manual/en/ref.classobj.php (class/object functions)
http://us3.php.net/manual/en/languag...reflection.php (Reflection
API)

Also note, in PHP4, classes have lower case names when you use
get_class(), even if defined with upper case letters. ie: MyClass =>
myclass.
In PHP5, it stays the same as declared.

Typo by Wayne: instanceof is available in PHP5, not PHP4.

Also be aware of differences between how objects are handled in PHP5 vs
PHP4.
PHP5 treats them more like java, they are all passed by reference.
PHP4 copies them all over the place (thus the use for $o = & new
Object() );
Search around in the manual and user comments for more specific and
detailed information.

-Richard Levasseur

Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.

Thanks again.

Wayne wrote:
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archimetrics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

*** Posted via a free Usenet account from http://www.teranews.com ***


Apr 20 '06 #4
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank wrote:
How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do
this:

if($obj typeof Iterator)
...


There is gettype function.

--
http://www.mgogala.com

Apr 21 '06 #5
On Thu, 20 Apr 2006 13:12:49 -0400, Joshua Frank
<jf****@archimetrics.remove-luncheon-meat.com> wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.


Googling is not too useful for something like this. Your best bet is
to download the CHM manual from php.net (if on Windows) and just look
around in it. For example, as others have pointed out, it has a whole
section on classes and objects.

In otherwords, RTFM! ;)

Later,

Apr 21 '06 #6
Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.


Instead of googling or trying, how about www.php.net -> documentation
-> <select language> -> Function reference -> Classes/Objects.

;-)

/Marcin
Apr 25 '06 #7
I actually tried googling with the site:php.net qualifier and still
didn't find this. So I have to say that at least this particular bit of
info was not readily googled. Now that I know what the syntax is, it's
easy to find, but without knowing in advance, the best I could think of
was to type in the relevant keywords from other languages, like typeof,
and that did not find anything for me.

Marcin Dobrucki wrote:
Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these
terms. I did try, really.


Instead of googling or trying, how about www.php.net -> documentation
-> <select language> -> Function reference -> Classes/Objects.

;-)

/Marcin

*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eric | last post: by
4 posts views Thread by ichor | last post: by
3 posts views Thread by Alberto | last post: by
1 post views Thread by Brien King | last post: by
11 posts views Thread by Jason Kendall | last post: by
2 posts views Thread by Andrew Robinson | last post: by
20 posts views Thread by rkk | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.