Object Extensions for PHP — Runtime Object Extension for PHP

Download Now

Object Extensions for PHP is a extension for PHP that allows for runtime extension of PHP objects as well as other features like runtime callback definitions.

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(&apos;object-ext.php&apos;);

class test extends CExtendable {

public function testing($output) { echo &quot;Hello, we received: $output&quot;; } }

$test = new test(); $test->testing(&apos;Sample&apos;); ?>

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(&apos;Another Sample&apos;);
echo &apos;I don\&apos;t like lower case characters: &apos; . 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

View all entries