Extend PHP error_log() Functionality

Throughout development many of us use the built-in error_log() function quite a bit. I know I sure do when I am debugging models, controllers, and the such but don't you hate it when you want to write the contents of an array or object to the error log? The error_log() function only accepts a string parameter so we have to use a little trick when we want to output array/object contents. We can accomplish this using only a few lines of code:

While this is nothing ground breaking, it's certainly a handy trick when you are trying to debug code that deals with arrays and objects.

Autoload Classes w/ PHP


Are you using an object oriented approach with PHP and finding that your memory usage is going through the roof with the more classes that you add? PHP's autoload() function enables you to automatically load classes when they are used which will greatly reduce the overhead of your application.

In addition to the basics of autoloading classes there is also a primitive example of exception handling which will be enough to get you to the point of catching exceptions instead of dealing with errors.

Let's start by cloning or forking the repository which is available here: https://github.com/niczak/PHP-Autoload-Example.

PHP Autoload Repository

The autoload() function in this example (found in controller.php, line #2) looks for classes in ./Classes/ with the extension .class.php so any classes you add to this example must follow that pattern, see Social.class.php for reference. There is a method in the Social class (validateString()) which does two things: 1) verify that all parameters have a value (or in other words, !empty) and 2) verify that all parameters being passed are in fact of type string. If either of those tests fail an exception is thrown and our controller catches it. Try modifying line 9 and removing one of the parameters, or just comment out line 9 and uncomment out line 11. Doing either of those things will result in an exception being thrown which will help illustrate what is going on behind the scenes with the try/catch logic.

I hope this helps shed some light on the concept of autoloading classes and using exceptions. If you have questions please feel free to leave them in the comments!

File Uploading With PHP

This example shows you how to build a file upload handler that lets users select a file they wish to upload along with a name for the file just in case they want to specify something other than the file name. The function then takes the data stream, moves the temporary file to a location of your choosing on the server, renames the file to match that of either the file name that was uploaded or if a name was provided on the input form it will rename it to that, adding underscores where spaces appear. Several error checks are performed along the way to ensure that everything happens as we expect it to. The following is a screenshot of the form itself after doing a submit, you will see the $_POST[] data along w/ the $_FILES[] data. This is helpful for both debugging as well as understanding what is going on behind the scenes.
PHP File Upload - Debug

In the $_POST[] array you will see two members, the first is a hidden form field value which defines the max file size and the second is 'sFile_Name' which is the value from the input field on the form. The $_FILES[] array contains the following information about the file: type [the mime type of the file uploaded], tmp_name [the location/name of the temporary file stored on the server], error [the error code reported by the server, 0 or UPLOAD_ERR_OK means no error occurred], and finally size [the size of the file].

Below you can see the renamed and processed file in the path we defined in our script, happily living on the server.
PHP File Upload - Files

Now for the code, everything is fairly documented and pretty straight forward. The biggest thing to keep in mind outside of the PHP code is that the form enctype must be set to "multipart/form-data" otherwise the file will not be submitted to the server. If you have any questions please leave them for me in the comments and I will reply as soon as I see them.