-
Using the _return() method on a callback
In this demo, we’re going to learn about the
_return()method available to Callbacks. To begin, let’s create a simple object with a method, and then let’s create the callback for it:<?php // object extensions library require('object-ext.php');
class test extends CExtendable {
public function testing($output) { echo "Hello, we received: $output"; } }$test = new test(); $test->testing('Sample'); ?>
Normally this will just output
Hello, we received: Sample. What if, however, you want to receive the output created by a function as a string, rather than have it output? This can be done with PHP’s buffering functionality, however, to do this every time you want to retrieve the output from a function can become quite complicated. This is where the_return()method comes into play. Let’s remove the last line from our previous example, and replace it with this instead:$callback = $test->_callback()->testing('Another Sample'); echo 'I don\'t like lower case characters: ' . strtoupper($callback->_return());
Obviously you would do something a little more useful with what is returned, but hopefully you get the point. A more practical application for the
_return()method would be for caching purposes. You could have a method that renders a database-driven list, for example, and you could easily retrieve the HTML produced by this method and cache to a file or whatever other caching method you want to use.Hopefully that gives you a few more ideas on what Object Extensions for PHP is capable of doing!
Posted on May 5th, 2006