Just needed to do some sanity checks, figure out some syntactical sugar (i.e. tricks), and test certain scenarios. I figured I’d share my findings with the world. Explanations are very light (brain dump) and embedded in the code; output follows directly. Oh, and this is a single stand-alone file.
php://stdin
Well, this really isn’t php://stdin, but the headline looks cool.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?php // look! multiple namespaces in the same file, couldn't use `namespace ns;` in this case, needed to use block encapsulation. namespace ns { // declare a namespace block interface INS { // declare a namespace } class NSMain implements INS { // declare a class that uses the interface } } namespace ns\sub { // declare a secondary namespace under the original one class NSSub { // and here's an empty class } } namespace other { // a third namespace (but a second top-level ns) class T { // the test class public static $s = array( // a static member full of class names 'DateTime', 'DOMDocument', 'ns\\NSMain', // using double-escaped notation, albeit optional 'ns\\sub\\NSSub' ); public $a = array( // a instance member full of class names 'DateTime', 'DOMDocument', 'ns\\NSMain', 'ns\\sub\\NSSub' ); } } namespace { // global namespace use other\T; // bring that test class in here from the third namespace "other", don't need to alias it with "as" $t = new T(); // we can instantiate it as if it were local to our namespace foreach ( $t->a as $className ) { // iterate through its class member "a" $temp = new $className(); // instantiate a class via a variable name (string) // dump some info about the class like if it exists, what its full namespaced path is and the object itself var_dump(class_exists($className,false),get_class($temp),$temp); echo '----'; } $is = $t instanceof T; // check if an object (class instance) is of a specific class type var_dump($is); $c = new T::$s[2](); // instantiate a variabled class name via a static member with an array index var_dump($c); // check if a class implements an interface via key lookup $implements = class_implements('ns\NSMain'); // get all implementations var_dump(isset($implements['ns\INS'])); // check if implemented } ?> |
php://stdout
Yes, this time it really is stdout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
boolean true string 'DateTime' (length=8) object(DateTime)[2] public 'date' => string '2013-11-16 08:44:57' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'UTC' (length=3) ---- boolean true string 'DOMDocument' (length=11) object(DOMDocument)[3] ---- boolean true string 'ns\NSMain' (length=9) object(ns\NSMain)[2] ---- boolean true string 'ns\sub\NSSub' (length=12) object(ns\sub\NSSub)[3] ---- boolean true object(ns\NSMain)[2] boolean true |