Bug #65150 [Nab]: DateTime subclasses expose extra properties
Edit report at https://bugs.php.net/bug.php?id=65150&edit=1
ID: 65150
Updated by: ab@php.net
Reported by: mike at silverorange dot com
Summary: DateTime subclasses expose extra properties
Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: CentOS or RHEL
PHP Version: 5.3.26
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-06-28 20:44:00] 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-28 18:55:35] 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 17:30:13] 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-27 21:04:03] 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"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65150&edit=1
Thread (5 messages)