typeof in PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua Frank

    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 ***
  • Wayne

    #2
    Re: typeof in PHP?

    On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
    <jfrank@archime trics.remove-luncheon-meat.com> wrote:
    [color=blue]
    >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[/color]

    You really didn't look hard enough!

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

    Comment

    • Joshua Frank

      #3
      Re: typeof in PHP?

      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:[color=blue]
      > On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
      > <jfrank@archime trics.remove-luncheon-meat.com> wrote:
      >[color=green]
      >> 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[/color]
      >
      > You really didn't look hard enough!
      >
      > In PHP4, use the is_a() function.
      > In PHP4, use the instanceof operator.
      >[/color]
      *** Posted via a free Usenet account from http://www.teranews.com ***

      Comment

      • Richard Levasseur

        #4
        Re: typeof in PHP?

        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:[color=blue]
        > 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:[color=green]
        > > On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
        > > <jfrank@archime trics.remove-luncheon-meat.com> wrote:
        > >[color=darkred]
        > >> 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[/color]
        > >
        > > You really didn't look hard enough!
        > >
        > > In PHP4, use the is_a() function.
        > > In PHP4, use the instanceof operator.
        > >[/color]
        > *** Posted via a free Usenet account from http://www.teranews.com ***[/color]

        Comment

        • Mladen Gogala

          #5
          Re: typeof in PHP?

          On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank wrote:
          [color=blue]
          > 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)
          > ...[/color]

          There is gettype function.

          --


          Comment

          • Wayne

            #6
            Re: typeof in PHP?

            On Thu, 20 Apr 2006 13:12:49 -0400, Joshua Frank
            <jfrank@archime trics.remove-luncheon-meat.com> wrote:
            [color=blue]
            >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.[/color]

            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,

            Comment

            • Marcin Dobrucki

              #7
              Re: typeof in PHP?

              Joshua Frank wrote:[color=blue]
              > 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.[/color]

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

              ;-)

              /Marcin

              Comment

              • Joshua Frank

                #8
                Re: typeof in PHP?

                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:[color=blue]
                > Joshua Frank wrote:[color=green]
                >> 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.[/color]
                >
                > Instead of googling or trying, how about www.php.net -> documentation
                > -> <select language> -> Function reference -> Classes/Objects.
                >
                > ;-)
                >
                > /Marcin[/color]
                *** Posted via a free Usenet account from http://www.teranews.com ***

                Comment

                Working...