Re: Re: Using DOM object, how?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ken Tozier wrote:

Rats. Looks like you can't encapsulate DOM objects in your own classes. I tried this and the browser 'view source' shows a completely empty document.

<?php
    $x = new MyDom();
    $x->createScriptElement('howdy.js');

    class MyDom
    {
        function MyDom()
        {
            $dom         = new DOMDocument('1.0', 'iso-8859-1');
            return $this;
        }

        function createScriptElement($inScriptPath)
        {
            $script    = $this->dom->createElement('script','');
            $script->setAttribute('language', 'javascript');
            $script->setAttribute('src', 'howdy.js');


            $this->dom->appendChild($script);

            echo $this->dom->saveXML();
        }
    }
?>

Made a few changes to it as follows and it works perfectly here (PHP 5.0.5, Gentoo Linux):

<?php
$x = new MyDom();
$x->createScriptElement('howdy.js');
print($x->saveXML());

class MyDom {
	private $dom;

	/* __construct() = PHP5 constructor */
	function __construct() {
		$this->dom = new DOMDocument('1.0', 'iso-8859-1');
		/* No need to return $this from a constructor */
	}

	function createScriptElement($scriptPath) {
		$script = $this->dom->createElement('script', '');
		$script->setAttribute('type', 'text/javascript');
		$script->setAttribute('src', $scriptPath);
		$this->dom->appendChild($script);
		/*
			Doesn't make sense for a createScriptElement()
			method to also print out the XML, so I made a
			separate method and called that from the
			mainline code.
		*/
	}

	function saveXML() {
		return $this->dom->saveXML();
	}
}
?>

--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux