InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Program: whereis in PHP
PHP

Program: whereis in PHP

InfinityCoder November 25, 2016

Although tools such as phpDocumentor provide quite detailed information about an entire series of classes, it can be useful to get a quick dump that lists all the functions and methods defined in a list of files.
The program in Example 7-3 loops through a list of files, includes them, and then uses the Reflection classes to gather information about them. Once the master list is compiled, the functions and methods are sorted alphabetically and printed out.

Example 7-3. whereis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
if ($argc < 2) {
    print "$argv[0]: classes1.php [, ...]\n";
    exit;
}
 
// Include the files
foreach (array_slice($argv, 1) as $filename) {
    include_once $filename;
}
 
// Get all the method and function information
// Start with the classes
$methods = array();
foreach (get_declared_classes() as $class) {
   $r = new ReflectionClass($class);
   // Eliminate built-in classes
   if ($r->isUserDefined()) {
       foreach ($r->getMethods() as $method) {
          // Eliminate inherited methods
          if ($method->getDeclaringClass()->getName() == $class) {
              $signature = "$class::" . $method->getName();
              $methods[$signature] = $method;
          }
       }
   }
}
// Then add the functions
$functions = array();
$defined_functions = get_defined_functions();
foreach ($defined_functions['user'] as $function) {
   $functions[$function] = new ReflectionFunction($function);
}
 
// Sort methods alphabetically by class
function sort_methods($a, $b) {
   list ($a_class, $a_method) = explode('::', $a);
   list ($b_class, $b_method) = explode('::', $b);
 
   if ($cmp = strcasecmp($a_class, $b_class)) {
       return $cmp;
   }
 
   return strcasecmp($a_method, $b_method);
}
uksort($methods, 'sort_methods');
 
// Sort functions alphabetically
// This is less complicated, but don't forget to
// remove the method sorting function from the list
unset($functions['sort_methods']);
// Sort 'em
ksort($functions);
 
// Print out information
foreach (array_merge($functions, $methods) as $name => $reflect) {
   $file = $reflect->getFileName();
   $line = $reflect->getStartLine();
 
   printf ("%-25s | %-40s | %6d\n", "$name()", $file, $line);
}

This code uses both the Reflection classes and also a couple of PHP functions, get_declared_classes() and get_declared_functions(), that aren’t part of the Reflection classes, but help with introspection.
It’s important to filter out any built-in PHP classes and functions; otherwise, the report will be less about your code and more about your PHP installation.

This is handled in two different ways. Because get_declared_classes() doesn’t distinguish between user and internal classes, the code calls ReflectionClass::isUserDefined() to check.

The get_defined_function() call, on the other hand, actually computes this for you, putting the information in the user array element.
Because it’s easier to scan the output of a sorted list, the script sorts the arrays of methods and functions.

Because multiple classes can have the same method, you need to use a user-defined sorting method, sort_methods(), which first compares two methods by their class names and then by their method names.
Once the data is sorted, it’s a relatively easy task to loop though the merged arrays, gather up the filename and starting line numbers, and print out a report.
Here are the results of running the PEAR HTTP class through the script:

1
2
3
4
HTTP::Date()                 | /usr/lib/php/HTTP.php          |     38
HTTP::head()                 | /usr/lib/php/HTTP.php          |    144
HTTP::negotiateLanguage()    | /usr/lib/php/HTTP.php          |     77
HTTP::redirect()             | /usr/lib/php/HTTP.php          |    186

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Uninstalling PEAR Packages in PHP
You wish to remove a PEAR package from your system. …

Uninstalling PEAR Packages in PHP

Generating Predictable Random Numbers in PHP
You want to make the random number generate predictable numbers …

Generating Predictable Random Numbers in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap