开发者

PHP: get_class_vars() vs. get_object_vars()

Regarding PHP, what are the differences between:开发者_运维问答

  1. get_class_vars()
  2. get_object_vars()


get_class_vars() takes the class_name get_object_vars() takes an $object variable

They both function similarly:

get_class_vars() will expose default public variables (or private/protected if called within the class) get_object_vars() will expose the current public variables (or private/protected if called within the class object)

Neither will expose methods.


As you can see from the get_class_vars and get_object_vars manual pages, get_class_vars gets the default values of properties of a class, and get_object_vars gets the current values of properties of an object.

Furthermore, get_class_vars takes a string (ie. the name of a class), whereas get_object_vars takes an object.

class Example
{
  public $var = 123;
}

$e = new Example();
$e->var = 456;

var_dump(get_class_vars("Example"));
/*
array(1) {
  ["var"]=>
  int(123)
}
*/

var_dump(get_object_vars($e));
/*
array(1) {
  ["var"]=>
  int(456)
}
*/


Theres one more thing:

get_object_vars() doesn't see static variables, but sees vars created during runtime!
get_class_vars() DOES SEE the static ones, but DOES NOT see new variables created during runtime, eg. in the constructor:

public function __construct() {$this->newval = "newval";}


As of PHP 5.5.9

I have noticed that there's actually a slight difference between get_class_vars and get_object_vars.

Regarding to PHP Bug Report, and I have tested it myself as well.

Both methods return the same result. BUT, you can manipulate object properties by using get_object_vars and not with get_class_vars.

Consider the following sample,

class Foo
{
  public $bar = null;
  public function __construct() {
    $this->bar = new DateTime(); // Now
    $this->far = &$this->bar;
  }
}
$foo = new Foo();
var_dump($foo);
$vars = get_object_vars($foo);
$vars['bar'] = new DateTime('2014-03-25');
var_dump($foo);

In the sample above, you will notice that the second var_dump will have $foo->bar modified.

Note: If you try to change the method from get_object_vars to get_class_vars, you will also notice that $foo->bar will not be modified.


get_class_vars() gives you even nonstatic variables, but their values are taken at the time of creating object, eg. init values.


I ran this very unscientific (and probably quite error prone) performance test:

I used this class:

class foo {
    public $bar;
    public $baz = "abcd";
    public $qux = 123;
    public $quux;
    public $quuz = "abcd";
    public $corge = 1234;
}

and did test 1 (get_object_vars):

$start = microtime(true);
for($i = 1; $i >= 100000; $i++)  {
    $foo = new foo;
    $gov = get_object_vars($foo);
}
$end = microtime(ture);

echo $end - $start;

then test 2 (get_class_vars - replacing the inside of the for loop):

$gcv = get_class_vars("foo");

then test 3 (get_class_vars, and assuming you need to instantiate the object anyway - again replacing the for loop):

$foo = new foo();
$gcv = get_class_vars("foo");

I ran each one 5 times and these are the results I got:

test 1:
0.00014019012451172
0.00014519691467285
0.00014495849609375
0.00012087821960449
0.00012421607971191
AVG: 0.0001350879669

test 2:
0.00013995170593262
0.00012898445129395
0.00010204315185547
0.00013494491577148
0.00061392784118652
AVG: 0.0002239704132
(if we remove the outlier): 0.0001264810562

test 3:
0.00014710426330566
0.00012397766113281
0.00012612342834473
0.00011992454528809
0.00014090538024902
AVG: 0.0001316070557

In test two there was a weird outlier in round 5... not sure why... hiccup on my server. If we remove that outlier, it looks like get_class_vars() is slightly more performant, regardless of whether you need to instantiate the class.

This test does not take into account any variable caching or php memory optimization that may be going on. Of course in a real world application it's highly unlikely that you'll iterate over either of the functions 10k times. So the functions' single and first execution performance is really more interesting, but much harder to test (accurately).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜