php - does var_dump() shows class properties?Get the solution ↓↓↓
If I use
var_dump to some class, say
<?php $date = new DateTime(); var_dump($date);
/var/www/php/test/index.php:5: object(DateTime) 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() shows both public and private properties.
For PHP up to 7.3
date was a public property of DateTime.
From PHP 7.4
date is a private property, and attempting to access it returns an
Undefined property message.
Note that these properties are not documented in the PHP manual for DateTime, so anything you do with them might break, without notice.
Share solution ↓
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.