A dynamic tool for referencing PHP modules and their associated routines, which are installed on your web-server.
**Note: Uses only 2 functions built-in to PHPs core and should be easy to convert to a later version of PHP. This current version uses PHP4.
Example:
PHP Installed Modules Dynamic Reference Tool
Abstract:
[/tab]As a PHP Developer, it is nice to know what functionality is available to you, and what you would have to implement yourself. Some of this functionality is provided for you by the PHP core, of which I am sure most of you are familiar with. However, a lot more may be added via Extension Modules installed with the PHP Web-Server (usually installed by your web-server administrator). Once these modules are installed, they provide a rich library of functions for your PHP scripts to access, giving you more flexibility and control as a developer.
[tab]The trick is in knowing what modules are installed with PHP on your web-server, what functionality those modules provide, and how to use that functionality in your scripts. All of this can be provided in the Dynamic Reference Tool we will create in this tutorial, which utilizes PHPs vast Online Documentation with its simple referencing standard!
By Rob J. Secord, B.Sc. (SystemWisdom)
Intended Audience:
Beginner to Advanced PHP Web-Application Developers.
Assumed knowledge:
-- PHP Basics
-- Arrays
-- HTML
Theory:
[/tab]Built-in to PHP are 2 functions that will assist us in creating our Dynamic Reference Tool, and they are: get_loaded_extensions() and get_extension_funcs(). These 2 functions are the heart of this tutorial and the Dynamic Reference Tool, and are rather simplistic to use!
[tab]Furthermore, we have the Official Online Documentation provided by PHP at php.net. The beauty lies within the simple referencing standard they provide, which is a URL in the format php.net/function_name_here. And considering they provide documentation for nearly every published PHP Module, this is a good thing!
[/tab]We will start by using the get_loaded_extensions() function, which returns to us an array of all the module names loaded with your web-servers installation of PHP. Using this array, we will build an HTML table to display all of the installed modules. We will then pass the module-name to the get_extension_funcs() function which will return to us an array of all of the function names contained within the module we specify. Again, we will build an HTML table to display all of the Module Functions. Finally, we will link each Module Function to PHPs Online Documentation for quick & easy reference!
Implementation:
[tab]We will start off by making our PHP file which we will call 'php_modules.php', you may call it anything you like. Once completed, you will place this file on your PHP web-server and run it as you would run any other PHP page. In page we will lay down a simple HTML/CSS framework to work from. It should look something like the following, refering to it any time during the next section:
CODE
<html>
<head>
<style><!--
body { color:#426394; }
table { border: 1px solid #426394; }
td.Seperator
{ border: solid #426394;
border-width: 0px 0px 1px 0px;
}
td.Text, a, a:hover
{ padding: 0px 5px 0px 5px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: normal;
font-style: normal;
font-size: 8pt;
color: #426394;
}
a:hover
{ background-color: #E3E3E3;
}
//--></style>
</head>
<body>
</body></html>
[/tab]You will notice a very basic HTML structure, with some simple CSS styles added to give some appeal to our page. If you don't know CSS you can safely omit the CSS completely by removing both 'style' tags and everything in between.
[tab]Moving on, we will now begin adding our PHP code to our page. First we need to think about the layout; one table will hold all of the module names, and for each module name there will be an associated table holding all of the function names (Please view my example posted near the top of this tutorial for a visual indication).
[/tab]If you will notice in my example, the Modules Table at the very top has 5 columns, and each table underneath has only 4 columns. This is a personal choice as I feel it looks more appealing, though it may be easily changed in your own scripts. The point to notice is that the PHP script itself only deals with 2 table structures; the 5 column one and the 4 column one (which is repeated). We will hold these values in 2 constant variables, so that they may be easily changed in the future:
CODE
<?php
$table1_cols = 5;
$table2_cols = 4;
?>
[tab]We will also need to create 2 Arrays to hold our lists of Modules and Functions, so we create those immediately as well:
CODE
<?php
$aModules = array();
$aModuleFuncs = array();
?>
[/tab]Now before going any further, I would like to first show you the completed code chunk which builds our Modules Table, and then we will walk through it together as I explain each step. Please have a good look at the following:
CODE
$counter = 1;
echo "<br><br><font size=+1><b><u>Listed Modules</u></b>:</font><br><br>\n";
echo "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
$aModules = get_loaded_extensions();
foreach( $aModules as $szModuleName )
{
// Check for First Column
if( $counter == 1 ) echo "\n <tr>";
// Output Column Data
echo "\n <td class=Text><a href=\"#$szModuleName\">$szModuleName</a></td>";
// Check for Last Column
if( $counter == $table1_cols )
{
echo "\n </tr>";
$counter = 0;
}
$counter++;
}
echo "\n</table>\n\n";
[tab]The first line from above is a 'counter' variable to keep track of which column we are currently outputting. This helps us determine when to output an opening table row tag (<tr>) and its corresponding closing tag (</tr>). The next 2 lines simply output a title "Listed Modules" and open the Table tag (<table>) with some default attributes.
[/tab]The 4th line from above is where we call the get_loaded_extensions() function which then returns to us an array populated with the names of all loaded modules. We store the array in our variable created from above named 'aModules'. Now that we have our array, it is a simple matter of outputting each value it contains via PHPs foreach loop.
[tab]So next we have our foreach() loop which I am sure you are all familiar with. We take each value in the array and assign it to the variable 'szModuleName' which we use to output the module name to HTML.
[/tab]Using the 'counter' variable mentioned earlier we will track which column we are currently outputting to; if it is the first column then we need to start by outputting an opening table row tag (<tr>) as in:
CODE
if( $counter == 1 ) echo "\n <tr>";
[tab]Next we get to output the actual module name, which we enclose in Table Data tags (<td>) with a style-class of 'Text' as defined by our CSS at the top. We will also want to link each Module Name with its respective Functions Table somewhere below the Modules Table, so we again enclose the actual module name with an Anchor Tag (<a>) which will be linked to a named-anchor (using the # symbol) elsewhere in our page. We will keep the name of the named-anchor the same as the module name for simplicity. This can all be seen in the following line of the code:
CODE
echo "\n <td class=Text><a href=\"#$szModuleName\">$szModuleName</a></td>";
[/tab]Next we will want to check to see if we are outputting to the last column, and if so, we simply output a closing Table Row tag (</tr>) to finish our current row, and reset the counter to 0 to generate a new row in our next pass of the foreach loop. After that, we are safe to increment the 'counter' variable by 1 in order to generate the next column of output. This can be seen in the following lines of code from above:
CODE
if( $counter == $table1_cols )
{
echo "\n </tr>";
$counter = 0;
}
$counter++;
[tab]Finally, to end the table after all of the module names have been output, we simply output a closing Table tag (</table>), as in:
CODE
echo "\n</table>\n\n";
[/tab]Well, that is that for the Modules Table, but now we need to create a table for each module containing all of its respective functions. This can be achieved in the exact same way as with the table we just created above. However, this time we will need to generate the tables with 4 columns instead of 5, so we would use the constant variable 'table2_cols' we defined at the start when checking for the last column with our 'counter' variable. Also, we will need to wrap all of what we have just done into another loop which will output 1 table for each loaded module.
[tab]We begin by outputting another simple title "Module Functions":
CODE
echo "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";
[/tab]Then we go right back into another foreach() loop of all of the loaded modules, but this time we are not just outputting the module name, we are also passing the module name as a string to the second PHP function called get_extension_funcs() which will return an array of all of the function names within the module. But first we should output a title for the table indicating which module we are listing functions from. This will also server as a location for each named-anchor we are linking to from the Modules Table above. We simply create an Anchor tag (<a>) and give it a name equal to that of the module name. All of this can be seen in the following code, and is considerably straight-forward:
CODE
// Output Title
echo "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";
// Modules Foreach Loop
foreach( $aModules as $szModuleName )
{
echo "\n<table border=0 cellpadding=0 cellspacing=0 width=100%>";
echo "\n <tr>\n <td class=\"Text Seperator\" colspan=\"$table2_cols\"><a name=\"$szModuleName\">Functions in <b>$szModuleName</b> Module..</a></td>\n </tr>";
// Loop through functions here..
echo "\n</table>\n<br><br>\n\n";
}
[tab]As you can see from above, each module will have its own table of functions with a header indicating which module the functions belong to, as well as a named-anchor for use with the named-anchor-links we created in the Modules Table at the beginning. The comment line where I stated "Loop through functions here.." is the spot where we will duplicate our Modules Table code with minor alterations. Again, I will post the whole code chunk first, and explain the differences below (includes the code chunk listed directly above, with the comment line replaced):
CODE
echo "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";
foreach( $aModules as $szModuleName )
{
echo "\n<table border=0 cellpadding=0 cellspacing=0 width=100%>";
echo "\n <tr>\n <td class=\"Text Seperator\" colspan=\"$table2_cols\"><a name=\"$szModuleName\">Functions in <b>$szModuleName</b> Module..</a></td>\n </tr>";
$counter = 1;
$aModuleFuncs = get_extension_funcs( $szModuleName );
foreach( $aModuleFuncs as $szFunctionName )
{
// Check for First Column
if( $counter == 1 ) echo "\n <tr>";
// Output Column Data
echo "\n <td class=Text><a href=\"http://php.net/$szFunctionName\" target=\"_new\">$szFunctionName</a></td>";
// Check for Last Column
if( $counter == $table2_cols )
{
echo "\n <tr>";
$counter = 0;
}
$counter++;
}
echo "\n</table>\n<br><br>\n\n";
}
[/tab]First thing to note is that the comment line has been replaced with a new code chunk that looks a lot like the very first foreach() loop we worked with. Again we have our 'counter' variable which servers the exact same purpose as before, but then we have our new function call to get_extension_funcs(). We pass the name of the current module, and it returns an array of function names, as in:
CODE
$aModuleFuncs = get_extension_funcs( $szModuleName );
[tab]We then enter into our nested foreach loop where we will receive the name of each function in the module stored in the 'szFunctionName' variable. With this variable, we can then output the name of the function much like before. The only notable difference this time, is that we enclose the output function name with an Anchor tag (<a>) linking to PHPs Online Documentation! But that's not all, we can even use the function name in the URL of the link to point directly to the documentation on each specific function! All of this can be seen in the following code chunk (as also posted above):
CODE
foreach( $aModuleFuncs as $szFunctionName )
{
// Check for First Column
if( $counter == 1 ) echo "\n <tr>";
// Output Column Data
echo "\n <td class=Text><a href=\"http://php.net/$szFunctionName\" target=\"_new\">$szFunctionName</a></td>";
// Check for Last Column
if( $counter == $table2_cols )
{
echo "\n <tr>";
$counter = 0;
}
$counter++;
}
[/tab]After you have that, you have a working "PHP Installed Modules Dynamic Reference Tool" that you can use and re-use on any PHP web-servers you may be developing on! To put it all together, you should have the following:
Final Completed Source Code Example:
CODE
<html>
<head>
<style><!--
body
{ color:#426394;
}
table
{ border: 1px solid #426394;
}
td.Seperator
{ border: solid #426394;
border-width: 0px 0px 1px 0px;
}
td.Text, a, a:hover
{ padding: 0px 5px 0px 5px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: normal;
font-style: normal;
font-size: 8pt;
color: #426394;
}
a:hover
{ background-color: #E3E3E3;
}
//--></style>
</head>
<body>
<?php
$table1_cols = 5;
$table2_cols = 4;
$aModules = array();
$aModuleFuncs = array();
$counter = 1;
echo "<br><br><font size=+1><b><u>Listed Modules</u></b>:</font><br><br>\n";
echo "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
$aModules = get_loaded_extensions();
foreach( $aModules as $szModuleName )
{
// Check for First Column
if( $counter == 1 ) echo "\n <tr>";
// Output Column Data
echo "\n <td class=Text><a href=\"#$szModuleName\" style=\"font-weight:bold;\">$szModuleName</a></td>";
// Check for Last Column
if( $counter == $table1_cols )
{
echo "\n </tr>";
$counter = 0;
}
$counter++;
}
echo "\n</table>\n\n";
echo "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";
//$aModules = get_loaded_extensions();
foreach( $aModules as $szModuleName )
{
echo "\n<table border=0 cellpadding=0 cellspacing=0 width=100%>";
echo "\n <tr>\n <td class=\"Text Seperator\" colspan=\"$table2_cols\"><a name=\"$szModuleName\">Functions in <b>$szModuleName</b> Module..</a></td>\n </tr>";
$counter = 1;
$aModuleFuncs = get_extension_funcs( $szModuleName );
foreach( $aModuleFuncs as $szFunctionName )
{
// Check for First Column
if( $counter == 1 ) echo "\n <tr>";
// Output Column Data
echo "\n <td class=Text><a href=\"http://php.net/$szFunctionName\" target=\"_new\">$szFunctionName</a></td>";
// Check for Last Column
if( $counter == $table2_cols )
{
echo "\n <tr>";
$counter = 0;
}
$counter++;
}
echo "\n</table>\n<br><br>\n\n";
}
?>
</body></html>
Working Example:
PHP Installed Modules Dynamic Reference Tool
Conclusion:
[tab]Well, I hope that you have learned something useful from this tutorial, and maybe even use such a tool when developing your web-based applications in PHP! Remember, this tool uses only 2 built-in PHP functions and should be a very easy conversion to later versions of PHP.
[/tab]Please feel free to comment on this tutorial, if you noticed anything wrong or out of place in this tutorial, please don't hesitate to mention it!
[tab]I am interested in all feedback really, I'm curious about what you think of my writing, tutorial, methods used, code, effectiveness, layout, etc.. Thanks!

