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
    22,335
    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);
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,335
    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
                    )
    
            )
    
    )
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11823 seconds
  • Memory Usage 2,905KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates