Skip to content

Instantly share code, notes, and snippets.

@turtlix
Forked from kylefarris/php-calling-class-test.php
Created December 13, 2018 06:49
Show Gist options
  • Save turtlix/60900be21d20a36c20f23c861e705f41 to your computer and use it in GitHub Desktop.
Save turtlix/60900be21d20a36c20f23c861e705f41 to your computer and use it in GitHub Desktop.

Revisions

  1. @kylefarris kylefarris revised this gist Mar 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ function get_calling_class() {
    $trace = debug_backtrace();

    // Get the class that is asking for who awoke it
    $class = $trace[1]['class'];
    $class = ( isset( $trace[1]['class'] ) ? $trace[1]['class'] : NULL );

    // +1 to i cos we have to account for calling this function
    for ( $i=1; $i<count( $trace ); $i++ ) {
  2. @kylefarris kylefarris revised this gist Mar 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ function get_calling_class() {

    // +1 to i cos we have to account for calling this function
    for ( $i=1; $i<count( $trace ); $i++ ) {
    if ( isset( $trace[$i] ) && isset( $traece[$i] ) ) // is it set?
    if ( isset( $trace[$i] ) && isset( $trace[$i]['class'] ) ) // is it set?
    if ( $class != $trace[$i]['class'] ) // is it a different class
    return $trace[$i]['class'];
    }
  3. @kylefarris kylefarris revised this gist Mar 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ function get_calling_class() {

    // +1 to i cos we have to account for calling this function
    for ( $i=1; $i<count( $trace ); $i++ ) {
    if ( isset( $trace[$i] ) ) // is it set?
    if ( isset( $trace[$i] ) && isset( $traece[$i] ) ) // is it set?
    if ( $class != $trace[$i]['class'] ) // is it a different class
    return $trace[$i]['class'];
    }
  4. @hamstar hamstar revised this gist Aug 3, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions php-calling-class-test.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /***********************************************************
    ******* SIMPLE TEST */
    ******* SIMPLE TEST **/

    class A {
    function t() {
    @@ -15,4 +15,4 @@ function x() {
    }

    $b = new B;
    $b->x();
    $b->x(); // prints B
  5. @hamstar hamstar revised this gist Aug 3, 2011. 2 changed files with 19 additions and 31 deletions.
    18 changes: 18 additions & 0 deletions php-calling-class-test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /***********************************************************
    ******* SIMPLE TEST */

    class A {
    function t() {
    echo get_calling_class();
    }
    }

    class B {
    function x() {
    $a = new A;
    $a->t();
    }
    }

    $b = new B;
    $b->x();
    32 changes: 1 addition & 31 deletions php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -14,34 +14,4 @@ function get_calling_class() {
    if ( $class != $trace[$i]['class'] ) // is it a different class
    return $trace[$i]['class'];
    }
    }

    /***********************************************************
    ******* SIMPLE TEST
    php> class A {
    ... function t() {
    ... echo get_calling_class();
    ... }
    ... }
    php> class B {
    ... function x() {
    ... $a = new A;
    ... $a->t();
    ... }
    ... }
    php> $b = newB; // LOL WHOOPS! phpsh assumes I'm inexperienced now...
    PHP Notice: Use of undefined constant newB - assumed 'newB' in /usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : eval()'d code on line 1
    Notice: Use of undefined constant newB - assumed 'newB' in /usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : eval()'d code on line 1
    php> $b = new B;
    php> $b->x();
    B
    //Yay it works!
    ********************************/
    }
  6. @hamstar hamstar revised this gist Aug 3, 2011. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,16 @@

    function get_calling_class() {

    $trace=debug_backtrace();
    //get the trace
    $trace = debug_backtrace();

    // Get the class that is asking for who awoke it
    $class = $trace[1]['class'];
    // +1

    // +1 to i cos we have to account for calling this function
    for ( $i=1; $i<count( $trace ); $i++ ) {
    if ( isset( $trace[$i] ) )
    if ( $class != $trace[$i]['class'] )
    if ( isset( $trace[$i] ) ) // is it set?
    if ( $class != $trace[$i]['class'] ) // is it a different class
    return $trace[$i]['class'];
    }
    }
  7. @hamstar hamstar created this gist Aug 3, 2011.
    44 changes: 44 additions & 0 deletions php-calling-class.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <?php

    function get_calling_class() {

    $trace=debug_backtrace();

    $class = $trace[1]['class'];
    // +1
    for ( $i=1; $i<count( $trace ); $i++ ) {
    if ( isset( $trace[$i] ) )
    if ( $class != $trace[$i]['class'] )
    return $trace[$i]['class'];
    }
    }

    /***********************************************************
    ******* SIMPLE TEST
    php> class A {
    ... function t() {
    ... echo get_calling_class();
    ... }
    ... }
    php> class B {
    ... function x() {
    ... $a = new A;
    ... $a->t();
    ... }
    ... }
    php> $b = newB; // LOL WHOOPS! phpsh assumes I'm inexperienced now...
    PHP Notice: Use of undefined constant newB - assumed 'newB' in /usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : eval()'d code on line 1
    Notice: Use of undefined constant newB - assumed 'newB' in /usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : eval()'d code on line 1
    php> $b = new B;
    php> $b->x();
    B
    //Yay it works!
    ********************************/