www.webdeveloper.com
Results 1 to 4 of 4

Thread: Regex Match and Retrieve Class Methods...

  1. #1
    Join Date
    Feb 2006
    Posts
    15

    Regex Match and Retrieve Class Methods...

    Hi guys,

    I really need your help, been trying to figure out how to do this.

    I want to retrieve the name of an object methods...but i just want the name...and i also don't want to retrieve __construct()


    PHP Code:
    $lines = array (
        [
    0] => 'public function __construct()';
        [
    1] => 'public function approved()';
        [
    2] => 'public function index($test)';
    ); 
    I don't know anything about regular expressions...

    i.e. from the line "public function approved()", I just want to retrieve "approved" and "public function index($test)", would get me just "index"

    It would be nice if it won't return me "__construct()"

    I really need help here...

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Easiest way would be to load the class definition and then use get_class_methods(). Stripping out the __construct() takes a few more lines.
    PHP Code:
    <?php
    require_once 'ErrorHandler.php';
    $methods get_class_methods('ErrorHandler');
    $methods array_values(
       
    array_filter(
          
    $methods,
          
    create_function(
             
    '$str',
             
    'return(strpos($str, "__construct") !== 0);'
          
    )
       )
    );
    print_r($methods);
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2006
    Posts
    15
    thanks your reply NogDog....

    however, our application is under MVC environment and almost all of our Controller class name is Admin (admin.php)....

    when i try to require_once() another admin.php from another module...it will give me an error....

    that is why I wanted to try to read the class file.....and search for methods...

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Here's an idea: calling the script via the CLI so that it never has more than one class defined at a time. (I'm trying to avoid a regexp solution, as it requires that you think of every syntax possibility, whereas this does not.)

    main script (can be called via URL):
    PHP Code:
    <?php
    // path to the CLI PHP executable on your server
    define('PHP_PATH''C:\wamp\bin\php\php5.2.9-2\php.exe');

    // path to the get_methods.php script that will get the methods
    define('SCRIPT_PATH''./get_methods.php');

    // array of classes to be processed
    // class_name => path/to/class_name.php
    $classes = array(
       
    'ErrorHandler' => './ErrorHandler.php',
       
    'RssDoc' => './RssDoc.php'
    );

    // let's do it
    $results = array();
    foreach(
    $classes as $class => $path) {
       
    $cmd sprintf(
          
    '%s -f %s %s %s',
          
    PHP_PATH,
          
    SCRIPT_PATH,
          
    $class,
          
    $path
       
    );
       
    $results[] = array(
          
    'class' => $class,
          
    'path' => $path,
          
    'methods' => unserialize(shell_exec($cmd)));
    }

    // show the results
    echo "<pre>";
    print_r($results);
    echo 
    "</pre>";
    The get_methods.php script which is called via CLI:
    PHP Code:
    <?php
    /**
     * Get the method names for a specified class
     * Meant to be called via command line (CLI)
     */

    // make sure we got enough args:
    if($argc 3) {
       
    $usage 'USAGE: /path/to/php ' __FILE__ ' ClassName /path/to/class_file.php';
       
    fwrite(STDERR$usage);
       return(
    0);
    }
    // do the parsing:
    require_once $argv[2];
    $methods get_class_methods($argv[1]);
    $methods array_values(
       
    array_filter(
          
    $methods,
          
    create_function(
             
    '$str',
             
    'return(strpos($str, "__construct") !== 0);'
          
    )
       )
    );
    // output serialized array as the result:
    echo serialize($methods);
    Output:
    Code:
    Array
    (
        [0] => Array
            (
                [class] => ErrorHandler
                [path] => ./ErrorHandler.php
                [methods] => Array
                    (
                        [0] => handler
                        [1] => message
                    )
    
            )
    
        [1] => Array
            (
                [class] => RssDoc
                [path] => ./RssDoc.php
                [methods] => Array
                    (
                        [0] => add2channel
                        [1] => addItem
                        [2] => output
                        [3] => createElement
                        [4] => createDocumentFragment
                        [5] => createTextNode
                        [6] => createComment
                        [7] => createCDATASection
                        [8] => createProcessingInstruction
                        [9] => createAttribute
                        [10] => createEntityReference
                        [11] => getElementsByTagName
                        [12] => importNode
                        [13] => createElementNS
                        [14] => createAttributeNS
                        [15] => getElementsByTagNameNS
                        [16] => getElementById
                        [17] => adoptNode
                        [18] => normalizeDocument
                        [19] => renameNode
                        [20] => load
                        [21] => save
                        [22] => loadXML
                        [23] => saveXML
                        [24] => validate
                        [25] => xinclude
                        [26] => loadHTML
                        [27] => loadHTMLFile
                        [28] => saveHTML
                        [29] => saveHTMLFile
                        [30] => schemaValidate
                        [31] => schemaValidateSource
                        [32] => relaxNGValidate
                        [33] => relaxNGValidateSource
                        [34] => registerNodeClass
                        [35] => insertBefore
                        [36] => replaceChild
                        [37] => removeChild
                        [38] => appendChild
                        [39] => hasChildNodes
                        [40] => cloneNode
                        [41] => normalize
                        [42] => isSupported
                        [43] => hasAttributes
                        [44] => compareDocumentPosition
                        [45] => isSameNode
                        [46] => lookupPrefix
                        [47] => isDefaultNamespace
                        [48] => lookupNamespaceUri
                        [49] => isEqualNode
                        [50] => getFeature
                        [51] => setUserData
                        [52] => getUserData
                        [53] => getNodePath
                        [54] => C14N
                        [55] => C14NFile
                    )
    
            )
    
    )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles