Skip to content

Instantly share code, notes, and snippets.

@DragonBe
Last active December 29, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DragonBe/7598105 to your computer and use it in GitHub Desktop.
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
<?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