php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65150 DateTime subclasses expose extra properties
Submitted: 2013-06-27 21:04 UTC Modified: 2013-06-29 12:02 UTC
From: mike at silverorange dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.26 OS: CentOS or RHEL
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mike at silverorange dot com
New email:
PHP Version: OS:

 

 [2013-06-27 21:04 UTC] mike at silverorange dot com
Description:
------------
I expected the DateTime class properties to be private or at least protected so 
users of my subclass don't get internals exposed.

See also https://bugs.php.net/bug.php?id=49382

Test script:
---------------
class MyDate extends DateTime
{
    public $extra = '';
}

$d = new MyDate('2013-01-01', new DateTimeZone('UTC'));
var_dump($d);

Expected result:
----------------
class MyDate#1 (4) {
  public $extra =>
  string(0) ""
}

Actual result:
--------------
class MyDate#1 (4) {
  public $extra =>
  string(0) ""
  public $date =>
  string(19) "2013-01-01 00:00:00"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(3) "UTC"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-28 17:30 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-06-28 17:30 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The properties are exposed intentionally, so that's ok. To achieve teh goal you've    
described the proper way were to use an appropriate design pattern, maybe Facade 
or Proxy.
 [2013-06-28 18:55 UTC] mike at silverorange dot com
The properties are not exposed on regular DateTime objects, only on subclasses.

The exposed properties are also read-only which is unintuitive for PHP userland 
developers.
 [2013-06-28 20:44 UTC] mike at silverorange dot com
The suggested design patterns are not appropriate in this instance because my 
object should be an instanceof DateTime. There is no common interface for DateTime 
that allows a proxy object.

The new DateTimeInterface in PHP 5.5 does not contain all the methods of DateTime.
 [2013-06-29 12:02 UTC] ab@php.net
The properties are exposed, otherwise there were nothing to see just doing 
var_dump(new DateTime). Agreed, that they're readonly is a bit weird, but isn't it 
exactly what you wanted (they to be protected)? And even private props will be 
var_dump'ed, even on non internal classes.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 01:01:29 2025 UTC