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

 

 

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";
}

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.