Jump to content
xisto Community
SystemWisdom

Php Installed Modules Dynamic Reference Tool An effective tool for coding in PHP

Recommended Posts

PHP Installed Modules Dynamic Reference Tool

 

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.

 

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

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

 

????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! :rolleyes:

 

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

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

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

 

????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).

 

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

<?php$table1_cols = 5;$table2_cols = 4;?>
????We will also need to create 2 Arrays to hold our lists of Modules and Functions, so we create those immediately as well:

<?php$aModules = array();$aModuleFuncs = array();?>
????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:

$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";
????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.

 

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

 

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

 

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

    if( $counter == 1 ) echo "\n <tr>";
????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:

echo "\n  <td class=Text><a href=\"#$szModuleName\">$szModuleName</a></td>";
????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:

    if( $counter == $table1_cols )    {        echo "\n </tr>";        $counter = 0;    }    $counter++;
????Finally, to end the table after all of the module names have been output, we simply output a closing Table tag (</table>), as in:

echo "\n</table>\n\n";

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

 

????We begin by outputting another simple title "Module Functions":

echo "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";
????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:

// Output Titleecho "<br><br><font size=+1><b><u>Module Functions</u></b>:</font>\n<br><br>\n";// Modules Foreach Loopforeach( $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";}
????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):

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";}
????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:

    $aModuleFuncs = get_extension_funcs( $szModuleName );
????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):

    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++;    }
????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:

<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:

 

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

 

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

Share this post


Link to post
Share on other sites

Very nicely written tutorial. I do would like to mention that this is not a tutorial for beginners, although you explain things well. the scripting type used here is more advanced then a beginner would do, making it confusing for the beginning PHP-scripters.Overal..great addition to the Xisto tutorials! :rolleyes:And for your great tutorials and dedication to Xisto i have given you a rep point, keep up the great work mate!

Share this post


Link to post
Share on other sites

This is an excellent and very useful tutorial. You have explained everything very well and make it very easy to understand. I have to agree with HmmZ however, that this is slightly too advanced for a coding beginner, but it is still a very good and well explained tutorial.

Share this post


Link to post
Share on other sites
:rolleyes: Thx guys, I'm glad you liked it! I guess I am doing good at writing tutorials (even though I always used to hate writing essays in school), so I will have to keep it up! :lol:Though I guess it isn't really a tutorial for beginners like you both have said (as far as the code goes), but the tool alone would be a good tool for beginners still learning PHP, to know what functionality they have access to! :lol:So for anyone who only wants the tool (ignoring the tutorial), feel free to copy & paste the source code into your own PHP page, place it on your web-server, and away you go!

Share this post


Link to post
Share on other sites

Hey, this is really nice. Good job.I don't think you have to worry about it not being for beginners. I mean, there are enough tutorials being posted for beginners here on Xisto (which is a good thing), but it's nice when someone posts something more advanced as well.I've played around with PHP some, but I'm hardly a guru, and I hadn't done anything with the get_loaded_extensions() function. And learning new things is always fun.So thanks. :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.