php - does var_dump() shows class properties?
Get the solution ↓↓↓If I usevar_dump
to some class, sayDateTime
:
<?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 gotUndefined property: DateTime::$date in /var/www/php/test/index.php on line 7
So how should I interpretvar_dump(some class)
?
Answer
Solution:
var_dump()
shows both public and private properties.
For PHP up to 7.3date
was a public property of DateTime.
From PHP 7.4date
is a private property, and attempting to access it returns anUndefined 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 ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
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.
Similar questions
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.