Variable scope or class problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Subhash

    Variable scope or class problem

    Hello,
    I have a Document class that stores the page url and raw content of
    page as downloaded from that url.

    The start() function creates an instance of Document class called $doc
    and calls process(). The process($doc) function downloads the page and
    stores the content in $doc->html and then calls the parse($doc)
    function. All goes well here.

    The problem is that when I try to echo the $doc->html in parse(),
    nothing is displayed! I am clueless as to what the problem is? Please
    help.

    Thanks!

    <?php

    // Document class
    class Document {
    var $url; // url of the document
    var $html; // raw content of page, as downloaded

    function Document($_url) {
    $this->url = $_url;
    }
    }

    // Main function
    function start() {
    $doc = new Document("http://www.amazon.com" );
    process($doc);
    }

    // Downloads and parses
    function process($doc) {
    if(download($do c)) {
    parse($doc);
    }
    }

    // Returns 1 on successful download else returns 0
    function download($doc) {
    if(! ($file=fopen($d oc->url,"r")) ) {
    return 0;
    }
    while (!feof($file)) {
    $doc->html = $doc->html . fread($file, 4096);
    }
    fclose($file);
    return 1;
    }

    // Parse the page, right now just echo the page
    function parse($doc) {
    echo $doc->html;
    }

    start();

    ?>
Working...