Wednesday 3 June 2015

PHP: I find the PHP equivalent of <cfdump> on GitHub shortly after I package it myself

G'day:
Sometimes I don't know why I bother. Oh well: at least I know more about Composer now. That's useful.

Forever, there's been a PHP implementation of <cfdump>, over at http://dbug.ospinto.com/. As good as that is, I didn't want to "install" it via downloading the file and sticking it into our codebase, I decided the better approach would be to work out how to package it up and install it via composer. That's why I was horsing around with Composer for the last coupla days ("PHP: trying (but not succeeding) to create my own Github-based Composer Package" and "PHP: Fixed! My colleague sorts that Composer issue out for me").



So I grabbed dBug.php and stuck it in a Github repo (not in a James Harvey sort of way, I acknowledged the authorship of the code appropriately), and worked out how to load it via Composer and all the rest of that bullshit. I was knocking together the test code for this article when I did a slightly different Google search than usual and... found the version of it already on GitHub. Sigh. It's over here: https://github.com/ospinto/dBug.

Oh well. Anyhow, in case yer a PHP person and don't know about <cfdump>, I'll write up the demo code anyhow.

Here we go:

// dump.php

require_once __DIR__ . '/../vendor/autoload.php';

$o = new dump\C('publicProperty value', 'protectedProperty value','privateProperty value');

$v = [
    'string' => 'a string',
    'object' => $o,
    'array' => $o->getProperties()
];

new dBug($v);

This outputs as follows:


Oh... C is defined thus:

// C.php

namespace dump;

class C {
    public $publicProperty;
    protected $protectedProperty;
    private $privateProperty;

    function __construct($publicProperty, $protectedProperty, $privateProperty){
        $this->publicProperty = $publicProperty;
        $this->protectedProperty = $protectedProperty;
        $this->privateProperty = $privateProperty;
    }

    public function getProperties(){
        return $this->getAsArray();
    }

    private function getAsArray(){
        return [
            'public' => $this->publicProperty,
            'protected' => $this->protectedProperty,
            'private' => $this->privateProperty
        ];
    }
}

It just demonstrates the difference in handling non-public elements of objects.

That shits all over var_dump():

array(3) {
  ["string"]=>
  string(8) "a string"
  ["object"]=>
  object(dump\C)#2 (3) {
    ["publicProperty"]=>
    string(20) "publicProperty value"
    ["protectedProperty":protected]=>
    string(23) "protectedProperty value"
    ["privateProperty":"dump\C":private]=>
    string(21) "privateProperty value"
  }
  ["array"]=>
  array(3) {
    ["public"]=>
    string(20) "publicProperty value"
    ["protected"]=>
    string(23) "protectedProperty value"
    ["private"]=>
    string(21) "privateProperty value"
  }
}

So... yeah... that's it. I dunno how I didn't find the GitHub version earlier. All in all it was a helpful exercise, and hopefully I can coerce my Team Lead to let us integrate this with our dev environment.

I'm also gonna work out how to implement this as a Twig Extension too... but first I'll google to see if it's been done already ;-)

--
Adam