John Davidson

php - does var_dump() shows class properties?

0 comments
Message:


If I use var_dump to some class, say DateTime:


<?php

$date = new DateTime();

var_dump($date);

I got


/var/www/php/test/index.php:5:
object(DateTime)[1]
public 'date' => string '2021-02-16 23:23:10.768097' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)

Which I interpret there are 3 public properties. So I try to access them:


<?php

$date = new DateTime();

var_dump($date);

//example of accessing public $date; of class DateTime;
echo $date->date;

but I got Undefined property: DateTime::$date in /var/www/php/test/index.php on line 7


So how should I interpret var_dump(some class)?



My Answer:
Yes, `var_dump()` will show the class properties of an object when used on an instance of a class. For example, if you use `var_dump(new DateTime())`, it will display all the properties and values of the DateTime object.

Rate this post

3 of 5 based on 5483 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us