469,631 Members | 1,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

is overloading () to make a callback class feasible in PHP?

I'm wondering if there would be a way to do such a thing as
overloading the () operator of a class in order to use that class as a
callback function. I presently would love to do this with the usort
function in order to easily pass runtime information to the custom
sort callback function. It has been my habit in the past(and with
other languages) to do this by using a class for my callback, setting
the information in the constructor(or any time before the cal really),
and then overloading () to give the class the facade of a function.
If this is posible in PHP please tell me. or, if there is a way to do
a similar thing that's more in keeping with the PHP idiom I'll listen
to that to. It would be useful to know this without all that mucking
about in documentation. If the no is resounding enough I'll write the
sort myself, but with a perfectly good sorting function just sitting
there it seems like a shame to have to do so.
Jul 17 '05 #1
2 1499
"justin allen" <ju************@gmail.com> wrote in message
news:d3*************************@posting.google.co m...
I'm wondering if there would be a way to do such a thing as
overloading the () operator of a class in order to use that class as a
callback function. I presently would love to do this with the usort
function in order to easily pass runtime information to the custom
sort callback function. It has been my habit in the past(and with
other languages) to do this by using a class for my callback, setting
the information in the constructor(or any time before the cal really),
and then overloading () to give the class the facade of a function.
If this is posible in PHP please tell me. or, if there is a way to do
a similar thing that's more in keeping with the PHP idiom I'll listen
to that to. It would be useful to know this without all that mucking
about in documentation. If the no is resounding enough I'll write the
sort myself, but with a perfectly good sorting function just sitting
there it seems like a shame to have to do so.


Use the array($obj, <name-of-callback>) convention. The following example
uses the Levenstein function to sort an array according to how similiar it
is to a specific text string.

<?

class LevenshteinSorter {
var $text;

function LevenshteinSorter($text) {
$this->text = $text;
}

function Compare($s1, $s2) {
$d1 = levenshtein($s1, $this->text);
$d2 = levenshtein($s2, $this->text);
return $d1 - $d2;
}
}

$names = array(
"I am not a crook",
"I don't like crooks",
"I have a dream",
"Chicken came first, damn it!",
"PHP is easy",
);

$L1 = new LevenshteinSorter("I am dreaming");
usort($names, array($L1, 'Compare'));

print_r($names);

$L2 = new LevenshteinSorter("I am a crook");
usort($names, array($L2, 'Compare'));

print_r($names);

?>

Jul 17 '05 #2
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<XJ********************@comcast.com>...
"justin allen" <ju************@gmail.com> wrote in message
news:d3*************************@posting.google.co m...
I'm wondering if there would be a way to do such a thing as
overloading the () operator of a class in order to use that class as a
callback function. I presently would love to do this with the usort
function in order to easily pass runtime information to the custom
sort callback function. It has been my habit in the past(and with
other languages) to do this by using a class for my callback, setting
the information in the constructor(or any time before the cal really),
and then overloading () to give the class the facade of a function.
If this is posible in PHP please tell me. or, if there is a way to do
a similar thing that's more in keeping with the PHP idiom I'll listen
to that to. It would be useful to know this without all that mucking
about in documentation. If the no is resounding enough I'll write the
sort myself, but with a perfectly good sorting function just sitting
there it seems like a shame to have to do so.


Use the array($obj, <name-of-callback>) convention. The following example
uses the Levenstein function to sort an array according to how similiar it
is to a specific text string.

<?

class LevenshteinSorter {
var $text;

function LevenshteinSorter($text) {
$this->text = $text;
}

function Compare($s1, $s2) {
$d1 = levenshtein($s1, $this->text);
$d2 = levenshtein($s2, $this->text);
return $d1 - $d2;
}
}

$names = array(
"I am not a crook",
"I don't like crooks",
"I have a dream",
"Chicken came first, damn it!",
"PHP is easy",
);

$L1 = new LevenshteinSorter("I am dreaming");
usort($names, array($L1, 'Compare'));

print_r($names);

$L2 = new LevenshteinSorter("I am a crook");
usort($names, array($L2, 'Compare'));

print_r($names);

?>

sweet. That's exactly what I needed. Thanks.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Terje Slettebø | last post: by
33 posts views Thread by Jacek Generowicz | last post: by
2 posts views Thread by Kamran | last post: by
31 posts views Thread by | last post: by
6 posts views Thread by TuxC0d3 | last post: by
3 posts views Thread by y-man | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.