NotionCommotion Posted July 27, 2016 Share Posted July 27, 2016 I am sure my use of the word "persistent" is wrong. Let me explain. Consider the following code. If I create a single element and then append it multiple times to another element, will each time it is appended be its own unique object, or will they all be the same object? Thanks $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $widget1s=[]; foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget1 ')]") as $img) { if( $id=$img->getAttribute('data-id')) { if(!in_array($id,$widget1s)) { $widget1s[]=$id; } $elem = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='widget1 w1'.$id; $elem->appendChild($class); $elem_value = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='value'; $elem_value->appendChild($class); $elem->appendChild($elem_value); $elem_units = $doc->createElement('span'); $class=$doc->createAttribute('class'); $class->value='units'; $elem_units->appendChild($class); $elem->appendChild($elem_units); $data_id=$doc->createAttribute('data-id'); $data_id->value=$id; $elem->appendChild($data_id); $img->parentNode->replaceChild($elem,$img); } } $widgets=[]; foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget2 ')]") as $img) { if( $id=$img->getAttribute('data-id')) { if(!in_array($id,$widget2s)) { $widget2s[]=$id; } $elem = $doc->createElement('div'); $class=$doc->createAttribute('class'); $class->value='widget2 w2'.$id; $elem->appendChild($class); $data_id=$doc->createAttribute('data-id'); $data_id->value=$id; $elem->appendChild($data_id); $img->parentNode->replaceChild($elem,$img); } } Quote Link to comment https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/ Share on other sites More sharing options...
kicken Posted July 27, 2016 Share Posted July 27, 2016 I'm not really sure what your asking. So long as you call createElement each iteration you'll be creating new unique element objects. If you tried to just re-use some element, such as: $elem = $doc->createElement('div'); foreach ($xpath->query('//something') as $something){ $something->appendChild($elem); } Then you'd end up with a document with only a single div appended to the last $something as each call to appendChild would just move it rather than duplicate it. If you're familiar at all with how DOM manipulation works in Javascript, then it works the same way in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/#findComment-1535187 Share on other sites More sharing options...
NotionCommotion Posted July 27, 2016 Author Share Posted July 27, 2016 I'm not really sure what your asking. So long as you call createElement each iteration you'll be creating new unique element objects. If you tried to just re-use some element, such as: Then you'd end up with a document with only a single div appended to the last $something as each call to appendChild would just move it rather than duplicate it. If you're familiar at all with how DOM manipulation works in Javascript, then it works the same way in PHP. Thanks Kicken, Was rushing out the door, and apologize for my cryptic post. Yes, I was kind of asking whether DOM manipulation is similar to JavaScript. For instance, would the following create <div><span>foo</span><span>bar</span></div> or <div><span>bar</span><span>bar</span></div>? Based on your response, I would expect the later. $div = $doc->createElement('div'); $span = $doc->createElement('span'); $span ->value='foo'; $div ->appendChild($span ); $span ->value='bar'; $div ->appendChild($span ); Quote Link to comment https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/#findComment-1535190 Share on other sites More sharing options...
kicken Posted July 27, 2016 Share Posted July 27, 2016 For instance, would the following create <div><span>foo</span><span>bar</span></div> or <div><span>bar</span><span>bar</span></div>? Neither, it would create: <div><span>bar</span></div> (->value should be ->nodeValue). The second appendChild essentially does nothing. $span can only exist at one place in the document. The first appendChild would place it as the first child of $div. The second appendChild would move it to the "second child of div" position, but that would remove it as the first-child meaning it's still just the first-child in the end. Quote Link to comment https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/#findComment-1535196 Share on other sites More sharing options...
NotionCommotion Posted July 28, 2016 Author Share Posted July 28, 2016 Thanks Kicken, Yes, I knew that, just didn't say that! Makes perfect sense (now). Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/#findComment-1535201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.