villermen Posted March 14, 2012 Share Posted March 14, 2012 Ok, the deal is as follows: I've got a function in a class.Within that function is another function called by uasort(), a custom array sorting function.The main function contains some variables which should be used in the sorting function.However I do not know how they can be accessed, and they can't be included as extra arguments in the function because the uasort only allows 2. Example: function somefunction() { $bar=5; $foo=4; function sorter($arg1,$arg2) { $bar & $foo should be accessed from here... } uasort($someArray,"sorter"); } If there's any way to access $bar & $foo from the sorting function please let me know. Gr.Viller Link to comment Share on other sites More sharing options...
justsomeguy Posted March 14, 2012 Share Posted March 14, 2012 I think that the only way is to use global variables. Is it not possible to define those variables inside the sort function? You could also make those variables properties of the class and also define the sort function as a class method, and then it will have access to the other properties and methods in the class. Link to comment Share on other sites More sharing options...
villermen Posted March 14, 2012 Author Share Posted March 14, 2012 Alright, I'll define those variables in the main class.I was just wondering if anything like that was possible, it would keep my code more logical/clean. Thanks again =) Link to comment Share on other sites More sharing options...
villermen Posted March 14, 2012 Author Share Posted March 14, 2012 Also, now I added that just a quick question.How do I reference to the new class function from the uasort's second argument? $bar$foofunction somefunction() { uasort($someArray,"???"); }function sorter($arg1,$arg2) { $this->bar $this->foo etc.. } Link to comment Share on other sites More sharing options...
justsomeguy Posted March 14, 2012 Share Posted March 14, 2012 There are examples on the manual page for usort that you should check, but you use an array to specify the object and method name. class Sort{ public $foo; public $bar; public function __construct() { $this->foo = 1; $this->bar = 2; } public function compare($a, $ { echo $this->foo . $this->bar; } public function do_sort() { uasort($array, array($this, 'compare')); }} $sorter = new Sort();$sorter->do_sort(); http://www.php.net/manual/en/function.usort.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.