SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking of class has method with specific name

    Hello!

    I'd just like to ask, if there is any way, that if you include/require some php file, which has class, is there any way to check if this class has specific method, without instancing this class ?

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without instancing the class I believe you can not, except if you file_get_contents of the php file and search for the method with preg_match.

    See also:
    http://www.php.net/method_exists

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is_callable(array('className', 'methodName'))
    might help

    otherwise look at reflection
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you are using PHP 5.2+ then you can use Reflection.
    PHP Code:
    $rc ReflectionClass'YourClassHere' );
    if ( 
    $rc->hasMethod'MethoidNameHere' ) )
        print 
    'Has method!'
    Or there is method_exists.
    PHP Code:
    if ( method_exists'YourClassHere''MethodNameHere' ) )
        print 
    'Has method!'
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Or there is method_exists.
    Except that method_exists requires that an object of the class has been instantiated doesn't it?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    Except that method_exists requires that an object of the class has been instantiated doesn't it?
    It does not. But you have to watch out it will report Private and Protected methods, unless that is what you want.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, stupid me, i should really do a bit more research before i ask for help next time. :P
    googling 'class method exists' did a trick hehe.

    And yeah, just tried 'method_exists', and it DOES NOT require intanceing, if anyone needs to know.

    Tnx for really quick help though! I really appriciate it!

  8. #8
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is_callable Mark mentioned sounds like it's worth checking out.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •