Re: Re: Using DOM object, how?

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

 




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();
        }
    }
?>

If the element adding code is in the same function as the dom create, it works
<?php
    $x = new MyDom();

    class MyDom
    {
        function MyDom()
        {
            $dom = new DOMDocument('1.0', 'iso-8859-1');
            $script    = $dom->createElement('script','');
            $script->setAttribute('language', 'javascript');
            $script->setAttribute('src', 'howdy.js');


            $dom->appendChild($script);

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

Also if the DOM object is declared outside the class, and class functions access it as a global it works
<?php

    $dom     = new DOMDocument('1.0', 'iso-8859-1');
    $x        = new MyDom();
    $x->createScriptElement('howdy.js');

    class MyDom
    {
        function MyDom()
        {
            return $this;
        }

        function createScriptElement($inScriptPath)
        {
            global $dom;

            $script    = $dom->createElement('script','');
            $script->setAttribute('language', 'javascript');
            $script->setAttribute('src', $inScriptPath);


            $dom->appendChild($script);

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

Would be nice if you could encapsulate it though...



You can probably add an empty text node to the script tag, or a text node consisting of a single space, or something like that, to make it use the long form.

--
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



--
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