Skip to content

Instantly share code, notes, and snippets.

@WesleyGoncalves
Last active May 22, 2020 19:01
Show Gist options
  • Save WesleyGoncalves/9b2fe427e0a811cb95f109265becf119 to your computer and use it in GitHub Desktop.
Save WesleyGoncalves/9b2fe427e0a811cb95f109265becf119 to your computer and use it in GitHub Desktop.
Test getting properties/attributes from object/class in PHP.
<?php
/**
* Test getting properties/attributes from object/class.
*
* It considers the following characteristics:
* visibility: public, protected, private
* abstraction: "normal", static, magic
* default value: with or without initial value
* updated value: whether or not its initial value is changed
*
* See the conclusion below.
*
* @author Wesley Gonçalves <dev@wesleygoncalves.com>
*
* @version 1.0.0
*
* @link https://gist.githubusercontent.com/WesleyGoncalves/9b2fe427e0a811cb95f109265becf119/raw/testGetPropertiesFromClass.php
*/
class Foo
{
public $public_NoInitialValue_NotChanged;
public $public_NoInitialValue_Changed;
public $public_InitialValue_NotChanged = 'A-c';
public $public_InitialValue_Changed = 'A-d';
public static $public_Static_NoInitialValue_NotChanged;
public static $public_Static_NoInitialValue_Changed;
public static $public_Static_InitialValue_NotChanged = 'A-g';
public static $public_Static_InitialValue_Changed = 'A-h';
protected $protected_NoInitialValue_NotChanged;
protected $protected_NoInitialValue_Changed;
protected $protected_InitialValue_NotChanged = 'B-c';
protected $protected_InitialValue_Changed = 'B-d';
protected static $protected_Static_NoInitialValue_NotChanged;
protected static $protected_Static_NoInitialValue_Changed;
protected static $protected_Static_InitialValue_NotChanged = 'B-g';
protected static $protected_Static_InitialValue_Changed = 'B-h';
private $private_NoInitialValue_NotChanged;
private $private_NoInitialValue_Changed;
private $private_InitialValue_NotChanged = 'C-c';
private $private_InitialValue_Changed = 'C-d';
private static $private_Static_NoInitialValue_NotChanged;
private static $private_Static_NoInitialValue_Changed;
private static $private_Static_InitialValue_NotChanged = 'C-g';
private static $private_Static_InitialValue_Changed = 'C-h';
/**
* @property mixed $magicProperty
*/
public function __construct()
{
$this->public_NoInitialValue_Changed = 'A-B';
$this->public_InitialValue_Changed = 'A-D';
static::$public_Static_NoInitialValue_Changed = 'A-F';
static::$public_Static_InitialValue_Changed = 'A-H';
$this->protected_NoInitialValue_Changed = 'B-B';
$this->protected_InitialValue_Changed = 'B-D';
static::$protected_Static_NoInitialValue_Changed = 'B-F';
static::$protected_Static_InitialValue_Changed = 'B-H';
$this->private_NoInitialValue_Changed = 'C-B';
$this->private_InitialValue_Changed = 'C-D';
static::$private_Static_NoInitialValue_Changed = 'C-F';
static::$private_Static_InitialValue_Changed = 'C-H';
$this->MagicProperty = 'M-A';
}
public function fromInside()
{
echo PHP_EOL . "------------- OBJECT VARS ------------------";
var_dump($props = get_object_vars($this)); // Conclusion: static: no && magic: yes && normal: changed
echo PHP_EOL . "------------- CLASS VARS ------------------";
var_dump($attr = get_class_vars(get_class($this))); // Conclusion: static: changed && magic: no && normal: not changed
echo PHP_EOL . "---------------------------------------------------------";
echo PHP_EOL . "------------- MERGING Object INTO Vars ------------------";
var_dump(array_merge($attr, $props)); // Conclusion: static: changed && magic: yes && normal: changed
}
}
(new Foo())->fromInside();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment