Re: can't reflection on DateTime properties?
| From: | Stanislav Malyshev | Date: | Sat, 04 Jun 2016 22:59:31 +0000 |
| Subject: | Re: can't reflection on DateTime properties? | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93792@lists.php.net to get a copy of this message | ||
Hi!
> I know that things like internal state and read-only properties are
> possible for classes written in C, but those features aren't possible in
> PHP code - classes that behave this way are inconsistent with classes
Of course they are possible. See __get/__set. But yes, internal classes
work a bit differently, which is no wonder since they can use internal
handlers which are not available to PHP code.
> That is no help at all, when you're implementing a JSON serializer.
Right, DateTime does not implement Serializable. Probably might be a
good idea to make it do that.
Otherwise - though in general it is not a very good idea to serialize
objects which didn't declare they are serializeable, especially internal
ones. But if one feels adventurous the handler to use would be
get_properties, and converting to array uses this handler, so you could
do that. It's the same handler serializers use to get properties, unless
Serializable is implemented or __sleep is defined.
> Because DateTime does not behave like other classes, the only
> work-around is to explicitly handle DateTime with an
> if/instanceof-statement and handle that particular class explicitly.
>
> There are plenty of work-arounds - the point is that this class doesn't
> behave consistently with any other PHP class.
It does. It's just PHP classes can behave in more ways than you think :)
And to some of the ways there's no good access from PHP space.
--
Stas Malyshev
smalyshev@gmail.com