PhpToolCase
Api Documentation Version 0.9.2
|
The PtcHandyMan component is a great tool that can assist you with autoloading classes and manage application paths.This class can be used to autoload class files saving a lot of time compared to including files manually. The tool can autoload classes based on custom naming conventions aswell, and will try to guess the file name to be loaded. The PtcHandyMan logs all actions to the PtcDebug class, making the process of debugging loaded classes a lot easear. This class can also be used to store and retrieve directory paths acting as an application paths manager. The component also supports autoloading classes with namespaces.
Main Features:
To start using the PtcHandyMan component to autoload our classes, we just need to register the autoloader:
To tell the autoloader where the files to be loaded reside, the methods PtcHandyMan::addFiles( ) and PtcHandyMan::addDirs( ) can be used. The following paragraphs will explain in details the usage of these methods.
Adding raw files to the autoloader can be convenient sometimes, here's how to do it:
Adding directories to the autoloader is quite usefull if you want to autoload all classes inside a directory. To add directories, the method PtcHandyMan::addDirs can be used:
The autoloader will load all classes that match the file names.
To add directories with classes that use namespaces, is very similar to adding directories:
As you can see, the only difference with the Adding Directories example, is the use of an array key to define the namespace.
Different Naming conventions can also be used to autoload classes, all we need to do is add sperators and naming conventions to the PtcHandyMan autoloader.
Here is an example using wordpress naming conventions to autoload classes:
As you can see naming convention can use 2 {placeholders}, which are {CLASS} for the class name and {SEP} for the separators added.
Here is another example that uses "class.{CLASS}"
as naming convention:
To retrieve the list of directories that the autoloader uses, the method PtcHandyMan::getDirs( ) can be used:
To retrieve only part of the directories, like just the files for example, we can use the "$type"
argument:
This are the possible options for the "$type"
argument:
The PtcHandyMan component also provides an elegant way to manage application paths, have a look at the following paragraphs to understand how this can be done.
The PtcHandyMan::addAppPaths( ) can store application paths inside an array, to be later retrieved by the unique name that we will set as array key.
Here's an example using "lib"
as unique identifier for the added path:
To retrieve previously added application paths, the method PtcHandyMan::getAppPaths( ) can be used. The following example will add a directoy to the autoloader, using a previously configured application path:
If the argument "$name"
is not used, all currently configured paths will be returned.
The PtcHandyMan component provides a couple of "handy methods"
, and one of them certainly is the PtcHandyMan::getProperty( ) method. This method can be used to read the value of a protected / private property from an object.
The following example shows how to read inaccessible properties:
The PhpToolCase
library, provides helper functions for the PtcHandyMan component. To start using these functions, the ptc-helpers.php file needs to be present and declared with the same namespace as the PtcHandyMan. The PtcHandyMan::register( ) method will include this file automatically.
See Helper Functions (ptc-helpers.php) for example usage of these library helpers.