Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: get_class_vars() vs. get_object_vars()

Tags:

php

Regarding PHP, what are the differences between:

  1. get_class_vars()
  2. get_object_vars()
like image 211
d-_-b Avatar asked Apr 13 '11 15:04

d-_-b


2 Answers

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)
}
*/
like image 85
Daniel Vandersluis Avatar answered Sep 28 '22 06:09

Daniel Vandersluis


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.

like image 21
ohmusama Avatar answered Sep 28 '22 06:09

ohmusama