PHP Programm Language Notes
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a general-purpose scripting language that is especially suited for web development. These are just some quick notes on various bits and bobs.
Page Contents
Check Your Code
PHP Synax checking
Just used the php -l <file>
to syntax check a file.
PHP Code Sniffer
PHP_CodeSniffer by Squiz Pty Ltd. (http://www.squiz.net). Helps detect and fix violations of a defined set of coding standards.
Autoloading Classes
Lets say you have a PHP file called jehtech.class.php
in which
the class jehtech
is defined. Rather than including it in
every script PHP can dynamically load it using a search pattern you
sepecify in the autoload()
function.
function my_autoload($class_name) { $php_file_name = $class_name . '.class.php'; $php_file_location = ...; // Find the file ... if (is_readable($php_file_location)) { include_once $php_file_location; } } ... ... spl_autoload_register('my_autoload'); // <--- You could use an anonymous func here
If your directory structure, for example, mirrored the name space you
could split the class name on the namespace seperator ('\'
)
and locate it that way.
Stream Filters
I know everyone frowns on using printf style debugging, but I still like it. What I had is debug output that has no indentation because it is harder to read. So here is a tiny little debug class that is a stream filter that you can associate with any PHP resource. When you print to it it will insert left-padding at the indent you are currently at.
A filter is a final piece of code which may perform operations on data as it is being read from or written to a stream. Any number of filters may be stacked onto a stream.
see: https://stackoverflow.com/questions/27103269/what-is-a-bucket-brigade class debug_filter extends \php_user_filter { function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); $space_bucket = stream_bucket_new($this->stream, " "); stream_bucket_prepend($out, $space_bucket); } return PSFS_PASS_ON; } } ... ... stream_filter_register("debug_filter", "\Tests\System\debug_filter") ... ... stream_filter_append(STDOUT, "debug_filter");