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.

9
  • Are you trying to replace the images while the object is serialized, then unserialize it? Commented Sep 16, 2009 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) Commented Sep 16, 2009 at 16:48
  • Helping you would be easier if you could add what var_dump($caching); prints. Commented Sep 16, 2009 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 Commented Sep 16, 2009 at 18:05
  • @Raynet: added the var_dump above - Hmm does that mean my caching class isn't returning a value? Commented Sep 16, 2009 at 18:17

1 Answer 1

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 );
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I gave up and replaced the string inside the Caching.php file and it works now... thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.