答えは簡単です。グローバルを使用しないでください。
その変数にアクセスし、その変数の値を変更する場合は、参照によってパラメーターとして渡します。
<?php
$testing = "hej";
function compare($b, $a, &$testing) {
$testing = "def";
}
compare(1, 2, $testing);
echo $testing; // result: "def"
値だけが必要な場合は、値で渡します。
<?php
$testing = "hej";
function compare($b, $a, $testing) {
$testing = "def";
}
compare(1, 2, $testing);
echo $testing; // result: "hej"
アップデート:
別のオプションは、オブジェクトをusort()
配列で渡すことです。
<?php
class mySort {
public $testing;
public function compare($a, $b) {
echo '<script>alert(\'>'.$this->testing.'<\');</script>';
}
}
$data = array(1, 2, 3, 4, 5);
$sorter = new mySort();
usort($data, array($sorter, 'compare'));