WCM Forum

WCM Forum (http://www.wcm.at/forum/index.php)
-   Programmierung (http://www.wcm.at/forum/forumdisplay.php?f=17)
-   -   PHP: using $this ... uasort (http://www.wcm.at/forum/showthread.php?t=161334)

Flink 15.03.2005 15:52

PHP: using $this ... uasort
 
PHP:
Wenn ich in der Compare-Funktion zu uasort auf $this->categories zugreife, erhalte ich die Fehlermeldung:
"Fatal error: Using $this when not in object context in"

Ich erhalte diese Fehlermeldung auch dann, wenn ich diese Funktion lokal als Unterfunktion in der Klasse selber aufrufe.

Ich rufe es auf mit:
Code:

uasort ($productsArray[$v], "categorycomp");
Ich habe folgendes probiert:
Code:

$categories1=array();                        // It is a copy of $this->categories to make asort possible
function categorycomp($row1, $row2)  {
global $categories1;
debug($this->categories1,'$this->categories1', __LINE__, __FILE__);
debug($categories1,'$categories1', __LINE__, __FILE__);
return strcmp($categories1[$row1['category']], $categories1[$row2['category']]);
}

Der Aufruf erfolgt in einer Funktion der Klasse
Code:

class tx_ttproducts {        var $categories=array();
D.h. auch wenn ich vor dem Funktionsaufruf $categories1=$this->categories zuweise, dann sind alle Werte von $categories1 in categorycomp leer.
Wie muß ich die $this-Variablen oder andere Variablen korrekt an die Compare-Funktion in uasort übergeben? :confused:

wenn ich es als Klassen-Member implementiere, erhalte ich:

"Warning: uasort() [function.uasort]: Invalid comparison function. in"

Code:

uasort ($productsArray[$v], "main_products::categorycomp");

Flink 15.03.2005 16:25

Ich habe jetzt eine Lösung gefunden:
Beim Aufruf
Code:

                                                global $categories1;
$categories1 = $this->categories;

schreiben.

Aber gibt es eine Möglichkeit, die Compare-Funktion als Klassen-Methode zu implementieren, damit ich direkt über $this->categories dort zugreifen kann?

käptn 16.03.2005 10:20

uasort(..., array(&$this, 'compareFunc'));

RTFM ;)

~

Flink 16.03.2005 11:07

Code:

uasort ($productsArray[$v], array(&$this, 'categorycomp'));
vielen Dank!

:bier:


Alle Zeitangaben in WEZ +2. Es ist jetzt 00:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 2009 FSL Verlag