Current location: Hot Scripts Forums » Programming Languages » PHP » Return list of functions & classes in a PHP file using PHP?


Return list of functions & classes in a PHP file using PHP?

Reply
  #1 (permalink)  
Old 06-25-09, 01:04 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Question Return list of functions & classes in a PHP file using PHP?

I'm wondering if there is a way to return list of functions & classes in a PHP file using PHP Can anybody help me please? Thanks.

Last edited by Mythvn; 06-25-09 at 01:06 AM.
Reply With Quote
  #2 (permalink)  
Old 06-25-09, 02:53 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Perhaps the Tokenizer helps here:

PHP: token_get_all - Manual
PHP: Examples - Manual

Other than that, you might be able to parse debug_backtrace().

And for a last suggestion, you could use regular expression to parse the function/class definitions.
Reply With Quote
  #3 (permalink)  
Old 06-25-09, 04:40 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
Perhaps the Tokenizer helps here:

PHP: token_get_all - Manual
PHP: Examples - Manual

Other than that, you might be able to parse debug_backtrace().

And for a last suggestion, you could use regular expression to parse the function/class definitions.
Hmm, after looking at the documentation, I think that token_get_all should work. I thought that REGEX may not accurate as it might be confused by string variables. If you get any idea of using REGEX please help me also . Thanks so much.
Reply With Quote
  #4 (permalink)  
Old 06-25-09, 04:47 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It'd be something like this:
PHP Code:

if (preg_match_all('~(function|class)\s+([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)~'$file_contents$matches))

{
    echo 
'<pre>'print_r($matchestrue), '</pre>';

Reply With Quote
  #5 (permalink)  
Old 06-25-09, 05:00 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
It'd be something like this:
PHP Code:

if (preg_match_all('~(function|class)\s+([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)~'$file_contents$matches))

{
    echo 
'<pre>'print_r($matchestrue), '</pre>';

PHP Code:

$file_contents = '

<?
$string 
"function file();";
?>
';
If I have such a variable like that, the string "file" will also be treated as a function.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code 2 convert .doc file into .pdf file jeephp PHP 7 11-03-10 04:31 AM
Write to php file with php script or create new file, it is possible? Oskare100 PHP 10 08-14-08 09:03 AM
PHP script that knows to read csv file ELV1I2 Script Requests 5 08-05-07 03:19 AM
php upload file script xavier039 PHP 5 08-29-06 04:35 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM


All times are GMT -5. The time now is 08:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.