By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,719 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,719 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
"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.