Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't add child. #152

Open
yusufusta opened this issue Nov 14, 2020 · 5 comments
Open

Couldn't add child. #152

yusufusta opened this issue Nov 14, 2020 · 5 comments

Comments

@yusufusta
Copy link

Hi, I want create a web-page with DiDOM but I have some problems.

<?php
require './vendor/autoload.php';
use DiDom\Document;

$Doc = new Document();
$Html = $Doc->createElement('html');
$Html->appendChild(
    $Doc->createElement('body')->appendChild(
        $Doc->createElement('h1', 'hi')
    )
);

echo $Html->html();
?>

Error:

PHP Warning:  DOMNode::cloneNode(): Couldn't fetch DOMElement in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php on line 105

Warning: DOMNode::cloneNode(): Couldn't fetch DOMElement in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php on line 105
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to DOMDocument::importNode() must be an instance of DOMNode, null given in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php:106
Stack trace:
#0 /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php(106): DOMDocument->importNode(NULL, true)
#1 /Users/yusufusta/Desktop/rasba/test.php(9): DiDom\Node->appendChild(Array)
#2 {main}
  thrown in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php on line 106

Fatal error: Uncaught TypeError: Argument 1 passed to DOMDocument::importNode() must be an instance of DOMNode, null given in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php:106
Stack trace:
#0 /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php(106): DOMDocument->importNode(NULL, true)
#1 /Users/yusufusta/Desktop/rasba/test.php(9): DiDom\Node->appendChild(Array)
#2 {main}
  thrown in /Users/yusufusta/Desktop/rasba/vendor/imangazaliev/didom/src/DiDom/Node.php on line 106
@Imangazaliev
Copy link
Owner

You have to store an each element to a variable:

<?php

use DiDom\Document;

require './vendor/autoload.php';

$doc = new Document();

$html = $doc->createElement('html');
$body = $doc->createElement('body');

$h1 = $body->appendChild($doc->createElement('h1', 'hi'));

$html->appendChild($body);

echo $html->html();

@yusufusta
Copy link
Author

Yes, with this way the problem will be solved but why? is there another way?

@Imangazaliev
Copy link
Owner

I don't know why it exactly happens but when you call

$Html->appendChild(
    $Doc->createElement('body')->appendChild(
        $Doc->createElement('h1', 'hi')
    )
);

it's try to add h1 element, not body because ->createElement('body')->appendChild(...) returns h1.

is there another way?

I don't know. I spent a little time to figure out what's wrong but I couldn't.

@yusufusta
Copy link
Author

yusufusta commented Nov 16, 2020

I don't know. I spent a little time to figure out what's wrong but I couldn't.

Okey, thanks. I guess there is no solution.

@yusufusta
Copy link
Author

I think this is an important issue and it should stay open.

@yusufusta yusufusta reopened this Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants