Last active
December 29, 2015 02:09
-
-
Save DragonBe/7598105 to your computer and use it in GitHub Desktop.
Change in the way get_object_vars behaves in PHP 5.4 and PHP 5.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractModel { | |
public function toArray($includeProtected = false) | |
{ | |
if ($includeProtected == true) { | |
return get_object_vars($this); | |
} | |
// Wrapping this object in a closure changes the lexical scope, which | |
// allows us to easily return only public properties. | |
$self = (array) $this; | |
$clean = array (); | |
foreach ($self as $key => $value) { | |
if (strstr($key, '*')) continue; | |
$clean[$key] = $value; | |
} | |
return $clean; | |
} | |
} | |
class MyOwnModel extends AbstractModel | |
{ | |
protected $a = "t"; | |
public $b = "c"; | |
protected $g = "d"; | |
public static $test = "Z"; | |
} | |
$m = new MyOwnModel(); | |
var_dump($m->toArray(true)); | |
var_dump($m->toArray()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment