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

classes and callback functions

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


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.