Jump to content
Larry Ullman's Book Forums

Chapter 7: Construct To Reference: Array($This, 'methodname')


Recommended Posts

Change the sorting algorithm based on the desired sort order:

if ($this->_order == 'ascending') { uasort($list, array($this,

➝ 'ascSort')); } else {

uasort($list, array($this, ➝ 'descSort'));

}

 

The second argument to the uasort() function should be the name of the function to use for the comparison. 

Because that function—ascSort() or descSort()—is defined as a method

in this current object, you can use this construct to reference it: array($this, 'methodName').

 

In chapter 7 I don't get the construct above here in BOLD, did I miss something ?

Why not use uasort($list, 'ascSort');

  • Upvote 1
Link to comment
Share on other sites

Sorry for the confusion. You need to provide a function reference to uasort(). If you just provided "ascSort" there, you'd get an error because there's no "ascSort" function in PHP. That method exists, but only within a class or object. So to tell uasort() about the right method to use, you have to provide the object instance and the method name, doing so using array().

Let me know if it's still unclear.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

×
×
  • Create New...