ARCHIVE NOTICE

My website can still be found at industrialcuriosity.com, but I have not been posting on this blog as I've been primarily focused on therightstuff.medium.com - please head over there and take a look!

Tuesday 28 July 2015

Write a destructor in PHP for fatal error handling

This just posted on stackoverflow.

I recently had trouble with this as I was trying to handle destruction specifically for the case where the server experiences a timeout and I wanted to include class data in the error log. I would receive an error when referencing &$this (although I've seen it done in a few examples, possibly a version issue or a symfony side-effect), and the solution I came up with was fairly clean:

    class MyClass
    {
        protected $myVar;

        /**
         * constructor, registers shutdown handling
         */
        public function __construct()
        {
            $this->myVar = array();

            // workaround: set $self because $this fails
            $self = $this;
            // register for error logging in case of timeout
            $shutdown = function () use (&$self) {
                $self->shutdown();
            };
            register_shutdown_function($shutdown);
        }

        /**
         * handle shutdown events
         */
        public function shutdown()
        {
            $error = error_get_last();
            // if shutdown in error
            if ($error['type'] === E_ERROR) {
                // write contents to error log
                error_log('MyClass->myVar on shutdown' . json_encode($this->myVar), 0);
            }
        }
     
        ...

Hope this helps!

Thursday 23 July 2015

Setting up Squiz Labs code sniffer for PHPStorm on Windows

PHPStorm is a brilliant piece of software that's highly extensible. One of the extensions that's particularly useful is Squiz Labs' PHP Code Sniffer. It wasn't quite apparent from other documentation I found online that it's really simple to set up for Windows! So with the help of a co-worker I put together the following tutorial:

To install the code sniffer for PHPStorm:

  • Download and install PHP for Windows
  • Download https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
  • Create a file called phpcs.bat in the C:\php directory
    @echo off
    "C:\php\php.exe" -f "C:\PATH_TO_DOWNLOADED_PHAR\phpcs.phar" -- %*
  • In PHPStorm
    • Open File->Settings
    • Select Languages & Frameworks->PHP
      • Add a local interpreter that points to C:\php\php.exe
      • Select Languages & Frameworks->PHP->Code Sniffer
      • Update the local configuration to point to C:\php\phpcs.bat
        • increase the Tool process timeout if you're working on remote code
    • Click Validate to ensure that the code sniffer is loaded correctly
    • Click Apply
    • Select Editor->Inspections
      • Select the checkbox for PHP Code Sniffer validation
      • Select PHP Code Sniffer validation
      • Set the coding standard (you'll probably want PSR2)
    • To run the code sniffer
      • Press CTRL + ALT + SHIFT + I
      • Type "code sniffer" and press enter
      • Select the current file
And there you go!