php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #68512 DOMDocument specific properties in child class not used.
Submitted: 2014-11-27 08:20 UTC Modified: 2021-03-12 17:45 UTC
From: arjen at react dot com Assigned:
Status: Verified Package: DOM XML related
PHP Version: 5.6.3 OS: Linux
Private report: No CVE-ID: None
 [2014-11-27 08:20 UTC] arjen at react dot com
Description:
------------
When extending DOMDocument, it's class properties cannot be overwritten/changed. DOMDocument's initial value are kept.

var_dump() however contains the actual new value, so behaviour is buggy at least.

See http://3v4l.org/6H88X

Test script:
---------------
<?php

class A extends DOMDocument 
{
    public $formatOutput = true;
}
$a = new A;

var_dump($a->formatOutput); // false
var_dump($a); // Has formatOutput => true

Expected result:
----------------
bool(true)
object(A)#1 (34) {
  ["formatOutput"]=>
  bool(true)
  <<cut>>
}

Actual result:
--------------
bool(false)
object(A)#1 (34) {
  ["formatOutput"]=>
  bool(true)
  <<cut>>
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-28 19:08 UTC] levim@php.net
I'm inclined to say the var_dump output is what is incorrect. I believe DOMDocument's properties are read-only by design and should remain that way.
 [2014-12-28 19:11 UTC] levim@php.net
Further examination of the manual shows that some of DomDocument's properties are explicitly read-only while others are not. I am not sure what the exact reason for this is. Assuming that none of the properties are actually writable, I propose changing the docs to list that they are all read-only properties.
 [2021-03-12 17:45 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-03-12 17:45 UTC] cmb@php.net
> When extending DOMDocument, it's class properties cannot be
> overwritten/changed. DOMDocument's initial value are kept.

The point is that the properties of DOMDocument are implemented
via the internal counterpart of __get().  If a class extends such
objects, the internal handlers of the parent are attached to the
subclass, and these are prioritized over potentially existing
properties.  I don't think this can be changed, and I'm afraid it
is not even possible to let the compiler trigger a warning or
error if one attempts to override such properties.

It seems to me that such classes should be final, but anyway, we
should document this.

> I believe DOMDocument's properties are read-only by design and
> should remain that way.

DOMDocument::$formatOutput is writable[1], and is supposed to be.
The manual is correct in this regard[2].

[1] <https://3v4l.org/9nTDP>
[2] <https://www.php.net/domdocument>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 15:01:30 2024 UTC