Jump to content

does this exist in a class?


slimboy007

Recommended Posts

<?php

class myclass {
    // constructor
    function myclass()
    {
        return(true);
    }

    // method 1
    function myfunc1()
    {
        return(true);
    }

    // method 2
    function myfunc2()
    {
        return(true);
    }
}

$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());

foreach ($class_methods as $method_name) {
    echo "$method_name\n";
}

?> 

The above example will output:

 

 

myclass

myfunc1

myfunc2

 

 

Link to comment
Share on other sites

dirty way. Ok, I'll show the proper method:

<?php 
class test{
public function test2(){
	echo "hello";
}
}
$test = new test();
if (method_exists(test,test2)){
echo "found method";
}
?>

 

The first parameter has to either be a string of name of the class you're testing or either an instance of the class. So in your example either 'test' or $test. The second parameter has to be a string, 'test2'.

 

if (method_exists($test, 'test2')){
echo "found method";
}

 

or

 

if (method_exists('test', 'test2')){
echo "found method";
}

Link to comment
Share on other sites

 

The first parameter has to either be a string of name of the class you're testing or either an instance of the class. So in your example either 'test' or $test. The second parameter has to be a string, 'test2'.

 

if (method_exists($test, 'test2')){
echo "found method";
}

 

or

 

if (method_exists('test', 'test2')){
echo "found method";
}

Forgot to mention that. Thanks, Alex.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.