stuffradio Posted April 8, 2008 Share Posted April 8, 2008 I have this code that I'm starting to work on. When I try and return it, it won't print anything. When I echo it, everything is echoed. <?php class Template { private $theme; private $themeContents; function getTheme() { if (!$_GET['theme'] || $_GET['theme'] == "default") { ob_start(); $theme = file_get_contents(STYLE_DIR . "default/index.tpl"); $themeContents = ob_get_contents(); ob_end_clean(); return $themeContents; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/ Share on other sites More sharing options...
trq Posted April 8, 2008 Share Posted April 8, 2008 Theres nothing in your buffer. No reason to be using one either. <?php class Template { private $theme; private $themeContents; function getTheme() { if (!$_GET['theme'] || $_GET['theme'] == "default") { return file_get_contents(STYLE_DIR . "default/index.tpl"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-511864 Share on other sites More sharing options...
stuffradio Posted April 8, 2008 Author Share Posted April 8, 2008 So can you explain to me when I should use the ob functions? Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-512132 Share on other sites More sharing options...
trq Posted April 8, 2008 Share Posted April 8, 2008 When you want to capture output into a buffer. file_get_contents does not output anything (it simply returns a string), so there is nothing to capture in a buffer. Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-512192 Share on other sites More sharing options...
Daniel0 Posted April 8, 2008 Share Posted April 8, 2008 You could use output buffering if your template file contained PHP code which had to be evaluated. Then you could use include() or require() instead of file_get_contents(). By doing so there would be output - output which you might have to capture using the output buffering functions. Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-512292 Share on other sites More sharing options...
stuffradio Posted April 8, 2008 Author Share Posted April 8, 2008 That's weird Thorpe... I tried exactly what you said but it outputs nothing. Oh well, I'll just use echo for now. Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-512466 Share on other sites More sharing options...
trq Posted April 8, 2008 Share Posted April 8, 2008 That's weird Thorpe... I tried exactly what you said but it outputs nothing. Oh well, I'll just use echo for now. Can we see your calling code? Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-512508 Share on other sites More sharing options...
stuffradio Posted April 10, 2008 Author Share Posted April 10, 2008 Sorry I didn't post again, what do you mean by calling code? Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-514149 Share on other sites More sharing options...
keeB Posted April 10, 2008 Share Posted April 10, 2008 Whatever is using your object. Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-514192 Share on other sites More sharing options...
stuffradio Posted April 12, 2008 Author Share Posted April 12, 2008 I think this is what you meant: $display = new Template(); $display->getTheme(); Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-515356 Share on other sites More sharing options...
trq Posted April 13, 2008 Share Posted April 13, 2008 I think this is what you meant: $display = new Template(); $display->getTheme(); Of course that won't output anything, as I said earlier, file_get_contents() returns a string, it does not output it. Try... <?php $display = new Template(); echo $display->getTheme(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-516006 Share on other sites More sharing options...
stuffradio Posted April 16, 2008 Author Share Posted April 16, 2008 Awesome stuff man, you da man Quote Link to comment https://forums.phpfreaks.com/topic/100099-solved-return-not-returning/#findComment-518217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.