PHP Life

March 23, 2007

How to use the deployment diagram in PHP to easily find files which identify classes

Filed under: PHP, Programming — Eugene @ 11:31 pm

Every programmer who uses object-oriented programming wishes for well documented classes. Well documented classes mostly imply a well-known static diagram of classes. Indeed, this diagram gives a general idea of classes and helps to easily find a class responsible for necessary action.

But what can we do if we need to find a file containing the definition of specific class, e.g. the one which is located in the static diagram?

PHP is structured so that it enables to include classes without their specific location as the require_once procedure does not require the indication of a full path. This is good and not so good on the other hand. The situation is rather usual when a parent class located somewhere else is indicated in the class identification:

class MyClass extends MyParentClass {}

Finding MyParentClass is impeded by the fact that its path is not clearly indicated anywhere. In that case a programmer just looks for MyParentClass in all system files.

This is a very long and inconvenient process, as a class can be found in many files (e.g. in 20 or 30) but still have no indicated path. As a result it takes a programmer up to an hour of his time just to find a necessary file.

Deployment diagram helps to improve the situation and reduce the search time. The purpose of diagram is to show which classes are identified in which files.

The deployment diagram is created when there is a need in:

  • Preliminary design of classes.

  • Documenting of already designed classes.


Let’s see the specific example.

The PHP library is usually made in the following way: a special directory is created where programmers put classes of a library. This directory is included into variable include_path, which is defined in php.ini file. For this deployment it is not necessary to indicate a full path to it in the script which uses the library classes.

Let include_path indicate /usr/local/apache/mylibs, and ? web-site root is located in the directory /usr/local/apache/htdocs.

Let us put myclass.php into the directory include_path (/usr/local/apache/mylibs), and ? calling script test.php into the directory /usr/local/apache/htdocs/.

To call myclass from the test.php script it is enough just to write a command require_once without indication of exact path:

require_once ‘myclass.php’;

Every library is located in its own directory in order not to overflow include_path with a large number of libraries.

Let us create a new class myclass2 and a library mylib2 where it will be located.

For this a subdirectory /usr/local/apache/mylibs/mylib2 is created in include_path. Let’s put myclass2.php file containing myclass2 definition into this subdirectory.

myclass2.php:

class myclass2 {}
?>

Then it is necessary to write the following command in the calling script /usr/local/apache/htdocs/test.php to include myclass2:

require_once ‘mylib2/myclass2.php’;

All these manipulations are perfectly reflected in UML deployment diagram.

Subdirectory is a package in UML notation, and it is shown in the diagram as

A file in UML notation is a component. It is shown as

Accordingly, mylib2 library will be shown in the deployment diagram in the following way:

This diagram means that in mylib2 directory there is myclass2.php file which identifies myclass2 (myclass2.php implements myclass2).

In order to reflect inclusion of myclass2.php from test.php script it is necessary to draw the following diagram:

This implies exactly the same as to write a command require_once ‘mylib2/myclass2.php’ in test.php script;

The general diagram will have the following layout:

Summary

Apparently UML deployment diagrams give a good visual idea of dependency between files and allow a new person to easily familiarize with them.

With the help of deployment diagram it is quite easy to find a file identifying a specific class.

Using the deployment diagrams will make the life a lot easier for you and your colleagues.

If you’d like to receive a notice when I prepare the list, please subscribe to my RSS feed (on the sidebar) or e-mail updates:

Enter your Email:

Digg the story

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.