Skip to content

Instantly share code, notes, and snippets.

@krakjoe

krakjoe/demo.php Secret

Created February 2, 2014 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/c6bcbb4e6d019c9297db to your computer and use it in GitHub Desktop.
Save krakjoe/c6bcbb4e6d019c9297db to your computer and use it in GitHub Desktop.
benefits of constructor behaviour
<?php
define('DEBUG', true);
function some_heavy_function(){ return 0; }
function some_other_heavy_function(){ return 1; }
function some_heavy_object_generator(){ return new stdClass; }
if (DEBUG) {
class Assertion {
public function __construct($expr) {
assert($expr);
}
}
class Debug {
protected $someObject;
public function __construct($someObject) {
$this->someObject = $someObject;
}
public function log() {
/* and so on */
}
}
} else {
class Assertion {}
class Debug {
public function log(){}
}
}
new Assertion(some_heavy_function() < some_other_heavy_function());
$debug = new Debug(some_heavy_object_generator());
$debug->log("something", "here");
?>
[L2 0x7fa474e64000 ZEND_SEND_VAL C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L2 0x7fa474e64030 ZEND_SEND_VAL C1 <unused> <unused> /usr/src/php-src/ctor.php]
[L2 0x7fa474e64060 ZEND_DO_FCALL C2 <unused> @0 /usr/src/php-src/ctor.php]
[L4 0x7fa474e64090 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L5 0x7fa474e640c0 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L6 0x7fa474e640f0 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L8 0x7fa474e64120 ZEND_FETCH_CONSTANT <unused> C6 @1 /usr/src/php-src/ctor.php]
[L8 0x7fa474e64150 ZEND_JMPZ @1 J11 <unused> /usr/src/php-src/ctor.php]
[L25 0x7fa474e64210 ZEND_DECLARE_CLASS C13 C14 @2 /usr/src/php-src/ctor.php]
[L27 0x7fa474e64240 ZEND_DECLARE_CLASS C15 C16 @3 /usr/src/php-src/ctor.php]
[L33 0x7fa474e64270 ZEND_FETCH_CLASS <unused> C17 @4 /usr/src/php-src/ctor.php]
[L33 0x7fa474e642a0 ZEND_NEW @4 <unused> @5 /usr/src/php-src/ctor.php]
[L35 0x7fa474e643c0 ZEND_FETCH_CLASS <unused> C21 @6 /usr/src/php-src/ctor.php]
[L35 0x7fa474e643f0 ZEND_NEW @6 <unused> @7 /usr/src/php-src/ctor.php]
[L35 0x7fa474e644b0 ZEND_ASSIGN $debug @7 @8 /usr/src/php-src/ctor.php]
[L37 0x7fa474e644e0 ZEND_INIT_METHOD_CALL $debug C25 <unused> /usr/src/php-src/ctor.php]
[L37 0x7fa474e64510 ZEND_SEND_VAL C27 <unused> <unused> /usr/src/php-src/ctor.php]
[L37 0x7fa474e64540 ZEND_SEND_VAL C28 <unused> <unused> /usr/src/php-src/ctor.php]
[L37 0x7fa474e64570 ZEND_DO_FCALL_BY_NAME <unused> <unused> @9 /usr/src/php-src/ctor.php]
[L28 0x7fa474e4a100 ZEND_RETURN C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L39 0x7fa474e645a0 ZEND_RETURN C29 <unused> <unused> /usr/src/php-src/ctor.php]
[L2 0x7f1379e64000 ZEND_SEND_VAL C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L2 0x7f1379e64030 ZEND_SEND_VAL C1 <unused> <unused> /usr/src/php-src/ctor.php]
[L2 0x7f1379e64060 ZEND_DO_FCALL C2 <unused> @0 /usr/src/php-src/ctor.php]
[L4 0x7f1379e64090 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L5 0x7f1379e640c0 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L6 0x7f1379e640f0 ZEND_NOP <unused> <unused> <unused> /usr/src/php-src/ctor.php]
[L8 0x7f1379e64120 ZEND_FETCH_CONSTANT <unused> C6 @1 /usr/src/php-src/ctor.php]
[L8 0x7f1379e64150 ZEND_JMPZ @1 J11 <unused> /usr/src/php-src/ctor.php]
[L9 0x7f1379e64180 ZEND_DECLARE_CLASS C9 C10 @2 /usr/src/php-src/ctor.php]
[L14 0x7f1379e641b0 ZEND_DECLARE_CLASS C11 C12 @3 /usr/src/php-src/ctor.php]
[L24 0x7f1379e641e0 ZEND_JMP J13 /usr/src/php-src/ctor.php]
[L33 0x7f1379e64270 ZEND_FETCH_CLASS <unused> C17 @4 /usr/src/php-src/ctor.php]
[L33 0x7f1379e642a0 ZEND_NEW @4 <unused> @5 /usr/src/php-src/ctor.php]
[L33 0x7f1379e642d0 ZEND_DO_FCALL C19 <unused> @6 /usr/src/php-src/ctor.php]
[L4 0x7f1379e55490 ZEND_RETURN C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L33 0x7f1379e64300 ZEND_DO_FCALL C20 <unused> @7 /usr/src/php-src/ctor.php]
[L5 0x7f1379e554f0 ZEND_RETURN C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L33 0x7f1379e64330 ZEND_IS_SMALLER @6 @7 @8 /usr/src/php-src/ctor.php]
[L33 0x7f1379e64360 ZEND_SEND_VAL @8 <unused> <unused> /usr/src/php-src/ctor.php]
[L33 0x7f1379e64390 ZEND_DO_FCALL_BY_NAME <unused> <unused> @9 /usr/src/php-src/ctor.php]
[L10 0x7f1379e3e0e0 ZEND_RECV <unused> <unused> $expr /usr/src/php-src/ctor.php]
[L11 0x7f1379e3e110 ZEND_SEND_VAR $expr <unused> <unused> /usr/src/php-src/ctor.php]
[L11 0x7f1379e3e140 ZEND_DO_FCALL C0 <unused> @0 /usr/src/php-src/ctor.php]
[L12 0x7f1379e3e170 ZEND_RETURN C1 <unused> <unused> /usr/src/php-src/ctor.php]
[L35 0x7f1379e643c0 ZEND_FETCH_CLASS <unused> C21 @10 /usr/src/php-src/ctor.php]
[L35 0x7f1379e643f0 ZEND_NEW @10 <unused> @11 /usr/src/php-src/ctor.php]
[L35 0x7f1379e64420 ZEND_DO_FCALL C23 <unused> @12 /usr/src/php-src/ctor.php]
[L6 0x7f1379e37300 ZEND_FETCH_CLASS <unused> C0 @0 /usr/src/php-src/ctor.php]
[L6 0x7f1379e37330 ZEND_NEW @0 <unused> @1 /usr/src/php-src/ctor.php]
[L6 0x7f1379e37390 ZEND_RETURN @1 <unused> <unused> /usr/src/php-src/ctor.php]
[L35 0x7f1379e64450 ZEND_SEND_VAR_NO_REF @12 <unused> <unused> /usr/src/php-src/ctor.php]
[L35 0x7f1379e64480 ZEND_DO_FCALL_BY_NAME <unused> <unused> @13 /usr/src/php-src/ctor.php]
[L17 0x7f1379e3e1a0 ZEND_RECV <unused> <unused> $someObject /usr/src/php-src/ctor.php]
[L18 0x7f1379e3e1d0 ZEND_ASSIGN_OBJ <unused> C0 @0 /usr/src/php-src/ctor.php]
[L19 0x7f1379e3e230 ZEND_RETURN C1 <unused> <unused> /usr/src/php-src/ctor.php]
[L35 0x7f1379e644b0 ZEND_ASSIGN $debug @11 @14 /usr/src/php-src/ctor.php]
[L37 0x7f1379e644e0 ZEND_INIT_METHOD_CALL $debug C25 <unused> /usr/src/php-src/ctor.php]
[L37 0x7f1379e64510 ZEND_SEND_VAL C27 <unused> <unused> /usr/src/php-src/ctor.php]
[L37 0x7f1379e64540 ZEND_SEND_VAL C28 <unused> <unused> /usr/src/php-src/ctor.php]
[L37 0x7f1379e64570 ZEND_DO_FCALL_BY_NAME <unused> <unused> @15 /usr/src/php-src/ctor.php]
[L22 0x7f1379e4a0a0 ZEND_RETURN C0 <unused> <unused> /usr/src/php-src/ctor.php]
[L39 0x7f1379e645a0 ZEND_RETURN C29 <unused> <unused> /usr/src/php-src/ctor.php]
@smalyshev
Copy link

You could also do:

<?php
class Assertion {
    public $debug = true;

    public function __construct($expr, $message) {
        if(!$this->debug) {
            return;
        }
        if($expr instanceof Closure) {
            assert($expr(), $message);
        } else {
            assert($expr, $message);
        }
    }
}

and then:

new Assertion(function() { return some_heavy_function() < some_other_heavy_function()); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment