Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

UK Web Hosting
UK Web Hosting
Website Hosting
Website Hosting
UK One Way
UK One Way
Get Top 10 Exposure
 
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008, 07:19 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default Tip : Ways to specify callback function in php

Here's the code demonstrating various ways in which you can specify callback functions in php, i will discuss them each in my next post :

Code:
<?php
  function sort_function($a, $b)
  {
    if($a < $b)
    {
     return -1;
    }
    else if($a == $b)
    {
      return 0;
    }
    else
    {
      return 1;
    }
  }
 
  class TestClass
  {
    public function sort_method($a, $b)
    {
      if($a < $b)
      {
        return -1;
      }
      else if($a == $b)
      {
        return 0;
      }
      else
      {
        return 1;
      }
    }
    
    public static function static_sort_method($a, $b)
    {
      if($a < $b)
      {
        return -1;
      }
      else if($a == $b)
      {
        return 0;
      }
      else
      {
        return 1;
      }
    }
  }
  
  $array1 = array(3, 1, 5, 23, 11, 17, -3);
  $array2 = array(3, 1, 5, 23, 11, 17, -3);
  $array3 = array(3, 1, 5, 23, 11, 17, -3);
  $array4 = array(3, 1, 5, 23, 11, 17, -3);
  
  print_r($array1);
  echo '<br />'; 
  usort($array1, 'sort_function'); 
  print_r($array1);
  echo '<br />';
  print_r($array2);
  echo '<br />'; 
  usort($array2, array(new TestClass(), 'sort_method')); 
  print_r($array2);
  echo '<br />';  
  print_r($array3);
  echo '<br />'; 
  usort($array3, array('TestClass', 'static_sort_method')); 
  print_r($array3);
  echo '<br />'; 
  print_r($array4);
  echo '<br />'; 
  usort($array4, create_function('$a, $b', '
  if($a < $b)
  {
    return -1;
  }
  else if($a == $b)
  {
    return 0;
  }
  else
  {
    return 1;
  }')); 
  print_r($array4);
  echo '<br />'; 
?>
Output produced by the code given above :

Quote:
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Array ( [0] => 3 [1] => 1 [2] => 5 [3] => 23 [4] => 11 [5] => 17 [6] => -3 )
Array ( [0] => -3 [1] => 1 [2] => 3 [3] => 5 [4] => 11 [5] => 17 [6] => 23 )
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 04-11-2008, 06:30 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default

First method to specify a callback function is to simply the pass the name of the function as a string.
Second method is used when you want to use an object's method as callback function. In that case, you pass a two element array as the argument, value at index 0 is the object itself and value at index 1 is the method name as string. Third method is used when you want to use a static class method as callback function. You pass a two element array here as well, value at index 0 is the class name as a string and value at index 1 is the class method name as string. Finally, you can also create anonymous callback functions by using create_function function.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 04-13-2008, 09:33 PM
Junior Member
 
Join Date: Mar 2008
Posts: 14
iTrader: 0 / 0%
webtoast is on a distinguished road
Default

Thanks for the read.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 06:39 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default

Quote:
Originally Posted by webtoast View Post
Thanks for the read.
You're welcome. Glad someone found it interesting.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK Web Hosting
UK WW SEO Tools
Free site submission
Web Directory
 
Advertisement
firewall script
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
adsense ads space buy banners changing host changing server channel solutions cheap off page packages cheap seo packages cheap seo services closing coupon code boss cart jv directory announcement domain name extension domain names e brochures ecommerce flash website promotion flash websites forum free directory google host quack hostquack inline price changes introduction ipn paypal payment boss job site joomla job link bid script hungarian link exchange linux host moving server off page seo packages picture of the day referral sell baners seo seo packages shared hosting submit url switching host thumbnail tld today in history traffic transfering host uk shipping postcodes web design webinare webmaster tools wordpress

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 05:41 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149