473,320 Members | 1,580 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,320 software developers and data experts.

classes and callback functions

Yesterday I prototyped some code to read an XML file, and convert it
into a web page, using the expat XML parser.

I then packaged it into a class, and found that there are problems with
using class member functions as callback functions. In this case calling
xml_set_object() works round the problem.

However, one of the features I want to add is configurable sorting of
the data, using usort() or similar. Am I right in thinking that I can't
use a class member function as a callback function in usort()?
--
Stewart Robert Hinsley
Nov 10 '08 #1
5 2704
Stewart Robert Hinsley schrieb:
However, one of the features I want to add is configurable sorting of
the data, using usort() or similar. Am I right in thinking that I can't
use a class member function as a callback function in usort()?
Of course you can:

class MyClass
{
public static function myStaticMethod() {}
public function myMethod() {}
}

$foo = new MyClass();

usort( $myArray, array( 'MyClass', 'myStaticMethod' ) );
usort( $myArray, array( $foo, 'myMethod' ) );

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Nov 10 '08 #2
Stewart Robert Hinsley wrote:
Yesterday I prototyped some code to read an XML file, and convert it
into a web page, using the expat XML parser.

I then packaged it into a class, and found that there are problems with
using class member functions as callback functions. In this case calling
xml_set_object() works round the problem.

However, one of the features I want to add is configurable sorting of
the data, using usort() or similar. Am I right in thinking that I can't
use a class member function as a callback function in usort()?
Unfortunately, usort() hasn't been "promoted" to work with objects yet.

If you don't need any other values from the class, Thomas's code will
work quite well. However, sometimes you do need other values - in which
case you have to use a "kludge" of keeping the object in a static member
or global variable. An example of doing it with a static member
variable (a bit long):
<?php
class MyClass { // Showing only necessary code for example
private $sortOrder = 'A'; // (A)scending or (D)escending)
private $myArray = array();
private static $sortObj; // Used to hold the object

function setSortOrder($s) {
if ($s == 'A' || $s == 'D') {
$this->sortOrder = $s;
return true;
}
else
return false;
}

function addElement($e) {
$this->myArray[] = $e;
}

function sort() {
self::$sortObj = $this;
usort($this->myArray, 'mySort');
self::$sortObj = null;
}

function myObjSort($a, $b) {
if ($this->sortOrder == 'A') // Ascending
{
if ($a $b)
return 1;
elseif ($a < $b)
return -1;
else
return 0;
} else {
if ($a < $b)
return 1;
elseif ($a $b)
return -1;
else
return 0;
}
}
static function getSortObject() {
return MyClass::$sortObj;
}
}

function mySort($a, $b) {
return MyClass::getSortObject()->myObjSort($a, $b);
}

$myObj = new MyClass();
$myObj->addElement('4');
$myObj->addElement('2');
$myObj->addElement('10');
$myObj->addElement('5');

$myObj->sort();
print_r($myObj);

$myObj->setSortOrder('D');
$myObj->sort();
print_r($myObj);

?>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '08 #3
Jerry Stuckle schrieb:
function sort() {
self::$sortObj = $this;
usort($this->myArray, 'mySort');
self::$sortObj = null;
}

function myObjSort($a, $b) {
if ($this->sortOrder == 'A') // Ascending
{
if ($a $b)
return 1;
elseif ($a < $b)
return -1;
else
return 0;
} else {
if ($a < $b)
return 1;
elseif ($a $b)
return -1;
else
return 0;
}
}
function sort() {
usort($this->myArray, array($this, 'myObjSort'));
}

Seems to work fine (PHP 5.2.0).

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Nov 10 '08 #4
Thomas Mlynarczyk wrote:
Jerry Stuckle schrieb:
> function sort() {
self::$sortObj = $this;
usort($this->myArray, 'mySort');
self::$sortObj = null;
}

function myObjSort($a, $b) {
if ($this->sortOrder == 'A') // Ascending
{
if ($a $b)
return 1;
elseif ($a < $b)
return -1;
else
return 0;
} else {
if ($a < $b)
return 1;
elseif ($a $b)
return -1;
else
return 0;
}
}

function sort() {
usort($this->myArray, array($this, 'myObjSort'));
}

Seems to work fine (PHP 5.2.0).

Greetings,
Thomas
That's interesting. I finally found it in the doc - it's only in an
example at the very end of the description of usort().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 11 '08 #5
In message <gf**********@registered.motzarella.org>, Jerry Stuckle
<js*******@attglobal.netwrites
>Thomas Mlynarczyk wrote:
>Jerry Stuckle schrieb:
>> function sort() {
self::$sortObj = $this;
usort($this->myArray, 'mySort');
self::$sortObj = null;
}

function myObjSort($a, $b) {
if ($this->sortOrder == 'A') // Ascending
{
if ($a $b)
return 1;
elseif ($a < $b)
return -1;
else
return 0;
} else {
if ($a < $b)
return 1;
elseif ($a $b)
return -1;
else
return 0;
}
}
function sort() {
usort($this->myArray, array($this, 'myObjSort'));
}
Seems to work fine (PHP 5.2.0).
Greetings,
Thomas

That's interesting. I finally found it in the doc - it's only in an
example at the very end of the description of usort().
Thanks to all for the advice. I find that the format

usort($this->myArray, array(&$this, 'myObjSort'));

works on the hosting company's installation (PHP 4.3.9).

I'll have to get a more up to date (but not too up to date) manual.
--
Stewart Robert Hinsley
Nov 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pratik | last post by:
what are callback functions? Where we require callback functions? In what scenario we require callback functions?
4
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
10
by: Bit byte | last post by:
I have a set of C++ classes for which I want to provide a C API - and compile into a C DLL, for use in an application that can only work with a C interface DLL. Any suggestions/pointers on how...
8
by: Lothar Behrens | last post by:
Hi, I am thinking about using classes to encapsulate threads for my application. My requirements are the following: The thread implementation sould not know what has to be implemented in...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.