method_exists for class methods 
Author Message
 method_exists for class methods

I want to determine if a class method exists. The PHP manual documents a
method called method_exists, but the first argument in a object not a
class from what I can tell. I want to have a object that may or may not
have a class method called init(). Now once I determine that the class
method exists I can simply:

MyClass::init();

and it will run the thing. But like I said, it may not exists (this is a
kind of dynamic loading plug-in thing, and not every object loaded will
have a init method). So what I want to do is:

if( method_exists( 'MyClass', 'init' ) ) {
    MyClass::init();

Quote:
}

Any suggestions?

--
Eric Anderson



Sun, 29 May 2005 09:27:37 GMT  
 method_exists for class methods

Quote:

> MyClass::init();

> and it will run the thing. But like I said, it may not exists (this is a
> kind of dynamic loading plug-in thing, and not every object loaded will
> have a init method). So what I want to do is:

> if( method_exists( 'MyClass', 'init' ) ) {
>    MyClass::init();
> }

> Any suggestions?

get_class_methods seems to be your friend:

<quote src='http://www.php.net/manual/en/function.get-class-methods.php'>
get_class_methods -- Returns an array of class methods' names

Description

array get_class_methods ( mixed class_name)

This function returns an array of method names defined for the class
specified by class_name .
</quote>

The only thing you have to do is:

if(array_key_exists('init',get_class_methods(MyClass)))
{
   MyClass::init();

Quote:
}

--

  Daniel Tryba



Sun, 29 May 2005 09:34:45 GMT  
 method_exists for class methods

Quote:

> have a init method). So what I want to do is:

> if( method_exists( 'MyClass', 'init' ) ) {
>     MyClass::init();
> }

> Any suggestions?

What you're doing is exactly right except that you need to pass an object
instead of a string for the first argument.

if( method_exists( $foo = new MyClass(), 'init' ) ) {...}

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Sun, 29 May 2005 21:18:02 GMT  
 method_exists for class methods

Quote:

> array get_class_methods ( mixed class_name)

> This function returns an array of method names defined for the class
> specified by class_name .
> </quote>

> The only thing you have to do is:

> if(array_key_exists('init',get_class_methods(MyClass))) {
>    MyClass::init();
> }

That helped out a lot. The 'init' method was in the list of methods
returned from get_class_methods. One odd thing is that when I tried to use
array_key_exists it still returned false. I finally just had it loop
through the array and use a if statement. Not sure why the
array_key_exists is not working. It may be some subtle bug in my code
somewhere. I will have to look at it some more when I get a chance.

Thanks for the pointer to the get_class_methods function though.

--
Eric Anderson



Mon, 30 May 2005 23:59:47 GMT  
 method_exists for class methods

Quote:

> What you're doing is exactly right except that you need to pass an object
> instead of a string for the first argument.

> if( method_exists( $foo = new MyClass(), 'init' ) ) {...}

The only problem is that I don't want to create a instance of it for
testing. Creating an instance has certain side effects because certain
resources are allocated.

--
Eric Anderson



Tue, 31 May 2005 00:00:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. accessing a instance method from a class method

2. How do I call a method from another method within the same class

3. Class methods vs Instance methods

4. Class method vs instance method???

5. Class method vs instance method???

6. Class methods in ab???.clw files and derived methods

7. same method as class and instance methods?

8. Calling protected methods from class methods

9. Singleton method == Class method ??

10. Static method and class method comments

11. Static method and class method comments

12. Class Methods (as opposed to Instance Methods)

 

 
Powered by phpBB® Forum Software