Hi all,

I was wondering if there is a way of getting the list of functions inside a class in PHP4.

This would be similar to the ReflectionClass in php5.

Say for example I have:

class testclass {

	
	function func1() {
		echo 'I am func1';
	}
	
	function func2() {
		echo 'I am func2';
	}

}

$ana = new Analysis('testclass');
$ana->getFunctionsList(); // should return array( 'func1', 'func2' );

Is there such a function in php4? or if someone knows of a way to get that information?

I was thinking of serializing the class, and then using regex to get the functions out of the serialized string, but there should be an easier way?

Thanks in advance :)

Dani AI

Generated

Short answer: if the class is already declared in the running script, use PHP's built-in introspection (as suggested) to get the method list. If you need to inspect source without executing it, parse the file with the tokenizer instead of fragile regex.

Example — get the methods for a loaded class and optionally remove inherited ones:

$methods = get_class_methods($className);

$parent = get_parent_class($className);
if ($parent) {
    $methods = array_values(array_diff($methods, get_class_methods($parent)));
}

When you cannot or do not want to include/require the file (to avoid side effects), tokenizing the source is safer than regex. The following extracts method names declared inside a named class without executing the file:

function extract_class_methods_from_file($file, $classname) {
    $src = file_get_contents($file);
    if ($src === false) return array();

    $tokens = token_get_all($src);
    $methods = array();
    $inClass = false;
    $depth = 0;

    for ($i = 0, $c = count($tokens); $i < $c; $i++) {
        $t = $tokens[$i];

        if (is_array($t) && $t[0] === T_CLASS) {
            // find class name and opening brace
            $j = $i + 1;
            while ($j < $c && (!is_array($tokens[$j]) || $tokens[$j][0] !== T_STRING)) $j++;
            $found = (isset($tokens[$j][1]) && $tokens[$j][1] === $classname);
            while ($j < $c && $tokens[$j] !== '{') $j++;
            if ($found) { $inClass = true; $i = $j; $depth = 1; continue; }
            $i = $j; continue;
        }

        if ($inClass) {
            if ($t === '{') $depth++;
            elseif ($t === '}') { if (--$depth === 0) break; }
            elseif (is_array($t) && $t[0] === T_FUNCTION) {
                // next T_STRING token is the method name
                $k = $i + 1;
                while ($k < $c) {
                    $u = $tokens[$k];
                    if (is_array($u) && $u[0] === T_STRING) { $methods[] = $u[1]; break; }
                    $k++;
                }
            }
        }
    }
    return $methods;
}

Notes: PHP4 has class-name constructors and no visibility keywords, so introspection will show all declared methods (including constructors and inherited ones). Parsing is the right choice if loading the file is undesirable. If upgrading to PHP5+ is an option, ReflectionClass provides a richer API.

Recommended Answers

All 2 Replies

Hi all,

I was wondering if there is a way of getting the list of functions inside a class in PHP4.

Try get_declared_classes() and get_class_methods(), and if needed get_defined_functions()...

With those, you can list all functions available within your actually running script.

commented: Awesome! Just what i needed. +1

Thanks alot man!
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.