2

Suppose there is a input file. The file contains php class with many functions and public variable.

I need a php script that would take that input file and count how many php function are there. This script will also print the function name like ..

public function hasMethod($method)
public function isVirtualField($field)

Can anyone post such php script to do this ?

2 Answers 2

4

get_class_methods() will give you all methods in a given object or class name, see PHP func ref

require_once('Model.php');
$methods = get_class_methods('Model');
print_r($methods);

Will show you all methods of the class Model in file Model.php

Sign up to request clarification or add additional context in comments.

4 Comments

+1 but you don't even need to create an instance of this class, get_class_methods() accepts a string (the class name), too.
The class is defined inside a file named, Model.php. I need to read from that file.
@soulmerge: Brilliant, didn't know that (although it's clear in the linked ref ;-)
@guru: see the updated answer, you just require() the file and run the function
1
class theclass {
    // constructor
    function theclass() {
        return;
    }

    function func1() {
        return;
    }

    // method 2
    function func2() {
        return;
    }
}

    $class_methods = get_class_methods('theclass');

foreach ($classFunctions as $functionName) {
    echo "$functionName\n";
}

1 Comment

I need to read from .php file. The Model class is defined in that Model.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.