Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Last active July 26, 2020 12:10
Show Gist options
  • Save surferxo3/0f6f181c2633996ff3815358d360f567 to your computer and use it in GitHub Desktop.
Save surferxo3/0f6f181c2633996ff3815358d360f567 to your computer and use it in GitHub Desktop.
Understanding difference between __CLASS__, get_class(), and get_called_class() with underlying self/static keyword
<?php
/*#############################
* Developer: Mohammad Sharaf Ali
* Designation: Web Developer
* Version: 1.0
*/#############################
class c1
{
public static $a = 1;
public function f()
{
echo 'get_class:: ' . get_class();
echo '<br />';
echo '__CLASS__:: ' . __CLASS__;
echo '<br />';
echo '__CLASS__ using get_class:: ' . get_class(new self());
echo '<br />';
echo 'get_called_class()::' . get_called_class();
echo '<br />';
echo 'get_called_class() using get_class::' . get_class(new static());
echo '<br />';
echo 'self:: ' . self::$a;
echo '<br />';
echo 'static:: ' . static::$a;
echo '<br />';
echo '<br />';
}
}
class c2 extends c1
{
public static $a = 2;
}
echo ':: $c1->f() output ::';
echo '<br />';
$c1 = new c1();
$c1->f();
echo ':: $c2->f() output ::';
echo '<br />';
$c2 = new c2();
$c2->f();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment