0

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.

Community
  • 1
  • 1
Mottie
  • 84,355
  • 30
  • 126
  • 241
  • Are you trying to replace the images while the object is serialized, then unserialize it? – null Sep 16 '09 at 16:45
  • So you did implement the __toString method in your Caching class? What does it look like? (Not needed when you call serialize first, but you'd better not change the contents of a serialized object) – Joost Sep 16 '09 at 16:48
  • Helping you would be easier if you could add what var_dump($caching); prints. – Raynet Sep 16 '09 at 17:05
  • I left the serialize function in there to show where I added it... I see now that I shouldn't be using serialize - I edited the post to show how I was using the TestClass from the link I mentioned – Mottie Sep 16 '09 at 18:05
  • @Raynet: added the var_dump above - Hmm does that mean my caching class isn't returning a value? – Mottie Sep 16 '09 at 18:17
  • @fudgey: yes, the class just returns the path to the saved cache file and the URL of the source webpage. – Raynet Sep 16 '09 at 19:17
  • @Raynet: Many thanks, errm, any chance you could look at my caching.php script (link above) and tell me what's wrong with it? Like I said I'm still learning php, mostly by example (I do know some jQuery and Javascript) and I find using "$this->" confusing. – Mottie Sep 16 '09 at 20:30
  • Try replacing $info in str_replace with $info->__toString(). This may help if you use PHP version <5.2. – Michał Niedźwiedzki Sep 16 '09 at 20:44
  • @Michał Słaby: Thanks but the site I use is running PHP Version 5.2.9 – Mottie Sep 16 '09 at 20:50

1 Answers1

1

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
  • 463
  • 6
  • 16
  • 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. – Mottie Sep 16 '09 at 18:19
  • I gave up and replaced the string inside the Caching.php file and it works now... thanks for your help! – Mottie Sep 17 '09 at 04:13