/    Sign up×
Community /Pin to ProfileBookmark

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()

[code=php]
$lines = array (
[0] => ‘public function __construct()’;
[1] => ‘public function approved()’;
[2] => ‘public function index($test)’;
);

[/code]

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…?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 18.2011 — Easiest way would be to load the class definition and then use get_class_methods(). Stripping out the __construct() takes a few more lines.

[code=php]
<?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);
[/code]
Copy linkTweet thisAlerts:
@farhan_ghazaliauthorFeb 18.2011 — 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...
Copy linkTweet thisAlerts:
@NogDogFeb 18.2011 — 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):
[code=php]
<?php
// path to the CLI PHP executable on your server
define('PHP_PATH', 'C:wampbinphpphp5.2.9-2php.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>";
[/code]

The get_methods.php script which is called via CLI:
[code=php]
<?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);
[/code]

Output:
<i>
</i>Array
(
[0] =&gt; Array
(
[class] =&gt; ErrorHandler
[path] =&gt; ./ErrorHandler.php
[methods] =&gt; Array
(
[0] =&gt; handler
[1] =&gt; message
)

<i> </i> )

<i> </i>[1] =&gt; Array
<i> </i> (
<i> </i> [class] =&gt; RssDoc
<i> </i> [path] =&gt; ./RssDoc.php
<i> </i> [methods] =&gt; Array
<i> </i> (
<i> </i> [0] =&gt; add2channel
<i> </i> [1] =&gt; addItem
<i> </i> [2] =&gt; output
<i> </i> [3] =&gt; createElement
<i> </i> [4] =&gt; createDocumentFragment
<i> </i> [5] =&gt; createTextNode
<i> </i> [6] =&gt; createComment
<i> </i> [7] =&gt; createCDATASection
<i> </i> [8] =&gt; createProcessingInstruction
<i> </i> [9] =&gt; createAttribute
<i> </i> [10] =&gt; createEntityReference
<i> </i> [11] =&gt; getElementsByTagName
<i> </i> [12] =&gt; importNode
<i> </i> [13] =&gt; createElementNS
<i> </i> [14] =&gt; createAttributeNS
<i> </i> [15] =&gt; getElementsByTagNameNS
<i> </i> [16] =&gt; getElementById
<i> </i> [17] =&gt; adoptNode
<i> </i> [18] =&gt; normalizeDocument
<i> </i> [19] =&gt; renameNode
<i> </i> [20] =&gt; load
<i> </i> [21] =&gt; save
<i> </i> [22] =&gt; loadXML
<i> </i> [23] =&gt; saveXML
<i> </i> [24] =&gt; validate
<i> </i> [25] =&gt; xinclude
<i> </i> [26] =&gt; loadHTML
<i> </i> [27] =&gt; loadHTMLFile
<i> </i> [28] =&gt; saveHTML
<i> </i> [29] =&gt; saveHTMLFile
<i> </i> [30] =&gt; schemaValidate
<i> </i> [31] =&gt; schemaValidateSource
<i> </i> [32] =&gt; relaxNGValidate
<i> </i> [33] =&gt; relaxNGValidateSource
<i> </i> [34] =&gt; registerNodeClass
<i> </i> [35] =&gt; insertBefore
<i> </i> [36] =&gt; replaceChild
<i> </i> [37] =&gt; removeChild
<i> </i> [38] =&gt; appendChild
<i> </i> [39] =&gt; hasChildNodes
<i> </i> [40] =&gt; cloneNode
<i> </i> [41] =&gt; normalize
<i> </i> [42] =&gt; isSupported
<i> </i> [43] =&gt; hasAttributes
<i> </i> [44] =&gt; compareDocumentPosition
<i> </i> [45] =&gt; isSameNode
<i> </i> [46] =&gt; lookupPrefix
<i> </i> [47] =&gt; isDefaultNamespace
<i> </i> [48] =&gt; lookupNamespaceUri
<i> </i> [49] =&gt; isEqualNode
<i> </i> [50] =&gt; getFeature
<i> </i> [51] =&gt; setUserData
<i> </i> [52] =&gt; getUserData
<i> </i> [53] =&gt; getNodePath
<i> </i> [54] =&gt; C14N
<i> </i> [55] =&gt; C14NFile
<i> </i> )

<i> </i> )

)
×

Success!

Help @farhan_ghazali spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...