473,605 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1634
"justin allen" <ju************ @gmail.com> wrote in message
news:d3******** *************** **@posting.goog le.com...
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 LevenshteinSort er {
var $text;

function LevenshteinSort er($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 LevenshteinSort er("I am dreaming");
usort($names, array($L1, 'Compare'));

print_r($names) ;

$L2 = new LevenshteinSort er("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******* *************@c omcast.com>...
"justin allen" <ju************ @gmail.com> wrote in message
news:d3******** *************** **@posting.goog le.com...
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 LevenshteinSort er {
var $text;

function LevenshteinSort er($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 LevenshteinSort er("I am dreaming");
usort($names, array($L1, 'Compare'));

print_r($names) ;

$L2 = new LevenshteinSort er("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
4708
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 manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
33
2515
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
2641
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
3012
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 I think my problem has to do with my lack of understandign of some issues in C++. There is a class in QWT called QwtPicker which allows one to make rubberbands and select part of the drawing canvas, very useful in zooming etc. Function...
31
2265
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
5399
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, vectors, operator overloading and what not.. And i was wondering if there is a way to override the global dereference operator, so to be able to check the address that one tries to dereference. This gives the ability to throw an exception when...
3
3270
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, obj2 ;
0
1677
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 application via TCP/IP. To handle the different rates of communication between the two external sources, a simple FIFO circular buffer is used, the class definition for which is below. This all works fine, EXCEPT when the app receives a message from the
0
1065
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 registering an event handler (or callback) to handle a certain type of event. Just out of interest, given that you have to put the event handler somewhere, why is it a problem to derive a new class and reimplement paintCell()?
0
8004
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8288
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.