473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1626
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: rich | last post by:
Hi all, I have a fairly complex "feed" application that recieves messages from an external user-supplied API via a callback function, and attempts to forward these messages to another...
0
by: David Boddie | last post by:
On Mon May 26 17:37:04 CEST 2008, Alex Gusarov wrote: Right. I vaguely remember someone showing something like this at EuroPython a couple of years ago. I believe that this approach is actually...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.