Skip to content

Instantly share code, notes, and snippets.

@hurie
Created July 16, 2014 06:40
Show Gist options
  • Save hurie/5f980a5a25b374de4bb4 to your computer and use it in GitHub Desktop.
Save hurie/5f980a5a25b374de4bb4 to your computer and use it in GitHub Desktop.
Trace static binding and method binding call
<?php
/**
* Created by PhpStorm.
* User: Azhar
* Date: 7/15/14
* Time: 10:59 AM
*/
$indent = 0;
function print_txt($text)
{
global $indent;
print str_repeat(' ', $indent) . $text . PHP_EOL;
}
class A
{
static function static1()
{
global $indent;
$indent++;
print_txt('A::static1(); ' . get_called_class() . '::static1();');
print_txt('');
$indent--;
}
static function static2()
{
global $indent;
$indent++;
print_txt('A::static2(); ' . get_called_class() . '::static2();');
print_txt('static1 - static');
static::static1();
print_txt('static1 - self');
self::static1();
$indent--;
}
static function static3()
{
global $indent;
$indent++;
print_txt('A::static3(); ' . get_called_class() . '::static3();');
print_txt('static2 - static');
static::static2();
print_txt('static2 - self');
self::static2();
$indent--;
}
function public1()
{
global $indent;
$indent++;
print_txt('A->public1(); ' . get_called_class() . '->public1();');
print_txt('');
$indent--;
}
function public2()
{
global $indent;
$indent++;
print_txt('A->public2(); ' . get_called_class() . '->public2();');
print_txt('static1 - static');
static::static1();
print_txt('static1 - self');
self::static1();
print_txt('public1 - $this');
$this->public1();
print_txt('public1 - self');
self::public1();
$indent--;
}
function public3()
{
global $indent;
$indent++;
print_txt('A->public3(); ' . get_called_class() . '->public3();');
print_txt('static2 - static');
static::static2();
print_txt('static2 - self');
self::static2();
print_txt('public2 - $this');
$this->public2();
print_txt('public2 - self');
self::public2();
$indent--;
}
}
class B extends A
{
static function static2()
{
global $indent;
$indent++;
print_txt('B::static2(); ' . get_called_class() . '::static2();');
print_txt('static1 - static');
static::static1();
print_txt('static1 - self');
self::static1();
print_txt('static2 - parent');
parent::static2();
$indent--;
}
static function static3()
{
global $indent;
$indent++;
print_txt('B::static3(); ' . get_called_class() . '::static3();');
print_txt('static2 - static');
static::static2();
print_txt('static2 - self');
self::static2();
print_txt('static3 - parent');
parent::static3();
$indent--;
}
function public2()
{
global $indent;
$indent++;
print_txt('B->public2(); ' . get_called_class() . '->public2();');
print_txt('static1 - static');
static::static1();
print_txt('static1 - self');
self::static1();
print_txt('public1 - $this');
$this->public1();
print_txt('public1 - self');
self::public1();
print_txt('public2 - parent');
parent::public2();
$indent--;
}
function public3()
{
global $indent;
$indent++;
print_txt('B->public3(); ' . get_called_class() . '->public3();');
print_txt('static2 - static');
static::static2();
print_txt('static2 - self');
self::static2();
print_txt('public2 - $this');
$this->public2();
print_txt('public2 - self');
self::public2();
print_txt('public3 - parent');
parent::public3();
$indent--;
}
}
print PHP_EOL . 'static2 - A' . PHP_EOL;
A::static2();
print PHP_EOL . 'static2 - B' . PHP_EOL;
B::static2();
print PHP_EOL . 'static3 - B' . PHP_EOL;
B::static3();
$a = new A();
print PHP_EOL . 'public2 - $a' . PHP_EOL;
$a->public2();
$b = new B();
print PHP_EOL . 'public2 - $b' . PHP_EOL;
$b->public2();
print PHP_EOL . 'public3 - $b' . PHP_EOL;
$b->public3();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment