views:

967

answers:

1

I'm trying to teach myself php... so please be kind and bear with me.

I'm trying to follow this tutorial on how to cache files... the page I want to cache is HTML only, so I've modified the php to just deal with data. I know the caching part is working, it's when I try to modify the results that I get a "Catchable fatal error: Object of class Caching could not be converted to string" in the str_replace line below.

I've tried using the __toString method here, and I've tried using serialize. Is there something I'm missing?

Edit: Oh and I've even tried casting operators.

 $caching = new Caching( "my.htm", "http://www.page-I-want.com/" );
 $info = new TestClass($caching);
 $info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );

My var_dump($caching); is as follows:

object(Caching)#1 (2) { ["filePath"]=>  string(9) "cache.htm" ["apiURI"]=>  string(27) "http://www.page-I-want.com/" }

Ok, I see now that the problem is with the caching.php not returning the value to the $caching string. Can anyone check out the link below and help me figure out why it's not working? Thanks!

I just posted my entire caching.php file here.

+1  A: 

The code in on the site you link works by downloading the page from URL you give and parse it for artists and then save them to the cache file. The cache-object only contains two variables; filePath and apiURI. If you want to modify how the page is parse and converted to the cached XML-file, you should change the stripAndSaveFile function.

Here is an example of how to modify the Caching.php to do what you wanted:

  function stripAndSaveFile($html) {
        //mange the html code in any way you want
        $modified_html = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $html );
        //save the xml in the cache
        file_put_contents($this->filePath, $modified_html);  
  }

Edit:

Other option is to extend the Caching class, in your php-code using the class you could do:

  class SpecialCaching extends Caching {
        var $html = "";
        function stripAndSaveFile($html) {
              //mange the html code in any way you want
              $this->html = $html;
        }
  }

  $caching = new SpecialCaching( "my.htm", "http://www.page-I-want.com/" );
  $info = $caching->html;
  $info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );
Raynet
I renamed the stripAndSaveFile function, and I didn't want to modify the data within the caching.php file because I will use it for other files and replace different objects - I added the function to the top post.
fudgey
I gave up and replaced the string inside the Caching.php file and it works now... thanks for your help!
fudgey