note 59778 added to function.is-callable
| From: | yetanotheruserathotmailetc at osu1 dot php dot net | Date: | Thu, 15 Dec 2005 14:47:10 +0000 |
| Subject: | note 59778 added to function.is-callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100197@lists.php.net to get a copy of this message | ||
I have come across a strange oddity in versions around the 4.3.11 mark - I may have missunderstood
the purpose of this function but hope this'll be helpful for some.
The point the code below is supposed to illustrate is that in some cases with
<? $myFunc = Array( $myObj, 'myMethod' ); ?>
<? is_callable( $myFunc, true, $callMe ); ?>
will return true, and give you $callMe set to myObj::myMethod but calling
<? $callMe(); ?>
doesn't work... however calling
<? $myFunc[0]->$myFunc[1](); ?>
seems to work fine..
... the reason all the code is down there is I think this oddity is due to how/the order in which
I've instantiated my classes or something...
anyhow... HTH someone! :-)
Code follows:
FILE 1 :
<?
include('myTools');
$foo = new myClass();
print $foo->getMySource();
class myClass{
var $flibble = 'wibble';
function myClass(
// Initialise loads of stuff.. including..
$this->tools = new myTools();
)
function getMySource(){
// This just returns the source.. ok, like some HTML to go into an email for example.
// Some arguments;
$args = $this->flibble;
// Call our Tool that returns the source..
$source = $this->tools->returnSource( Array ( $this, 'someHTML' ), $args );
// and return it..
return ( $source );
}
function someHTML($args){
// Leave PHP
?>
Here is some HTML.. that we want to build outside a PHP block,
possibly just cos it's tidier in <B>your favourite text editor</B>..
.. or you want this function to be elsewhere.. for your designers
to play with etc.. ... incidentally I'd like to say "<?=$args?>" etc.
<?
// .. and we're back.
}
}
?>
FILE 2:
<?
/* OK - this is some other big library and a whole load more
faff but this is the particular function in question... it
just calls the function it's been asked to and uses an output
buffer to grab the output and return it as a string rather than
letting it go to terminal/browser.... useful for grabbing PHP pages
for spidering, emailing etc. etc. etc..
*/
class myTools(){
function returnSource($func, $args){
if ( is_callable ( $func, true, $callMe ){
// Start a buffer
ob_start();
// Calling the function like this DOESN'T seem to work:
// ~~~~~~~~
// "Command not found : myClass::someHTML"
// $callMe($args);
// ~~~~~~~~
// But - what I've discovered is that this behaves fine..
if ( is_array($func) ){
$func[0]->$func[1]($args);
} else {
$func($args);
}
// Then we just carry on with our code..
$rtn = ob_get_contents();
ob_clean();
return ( $rtn );
} else {
error_log("Doh!");
}
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.is-callable.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59778
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59778&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59778&report=yes
Search -- http://master.php.net/manage/user-notes.php