Functions in PHP: Part 1 - Writing Your Own Functions

Functions in PHP: Part 1 - Writing Your Own Functions

John Kary discusses 4 guidelines that if followed will result in clear, focused functions that are easy to read, reason about and reuse. Examples are shown in PHP.

D3e3f4ac37c02289f5dfed115949fc88?s=128

John Kary

July 29, 2015
Tweet

Transcript

  1. unctions in PHP Part 1: Writing Your Own Functions ƒ

  2. <?php ! $hash = array( 'username' => 'john', 'food' =>

    'bacon', ); ! echo http_build_query($hash); ! // The above code would output... username=john&food=bacon
  3. <?php ! $hash = array( 'username' => 'john', 'food' =>

    'bacon', ); ! echo http_build_query($hash); ! // The above code would output... username=john&food=bacon
  4. function sum($foo, $bar) { return $foo + $bar; }

  5. function sum($foo, $bar) { return $foo + $bar; } echo

    sum(2, 2);
  6. function sum($foo, $bar) { return $foo + $bar; } echo

    sum(2, 2); 4
  7. function sum($foo, $bar) { return $foo + $bar; } echo

    sum(2, 2); 4 echo sum(16, 8);
  8. function sum($foo, $bar) { return $foo + $bar; } echo

    sum(2, 2); 4 echo sum(16, 8); 24
  9. Blocks of Reusable Code

  10. Blocks of Reusable Code Easy to read and digest

  11. Blocks of Reusable Code Easy to read and digest Easier

    to Maintain
  12. Anatomy! of Functions

  13. function announce_sum($foo, $bar) { $sum = $foo + $bar; return

    $sum; } Function Keyword
  14. function announce_sum($foo, $bar) { $sum = $foo + $bar; return

    $sum; } Name
  15. function announce_sum($foo, $bar) { $sum = $foo + $bar; return

    $sum; } Arguments
  16. function announce_sum($foo, $bar) { $sum = $foo + $bar; return

    $sum; } Body
  17. function announce_sum($foo, $bar) { $sum = $foo + $bar; return

    $sum; } Return
  18. None
  19. Given

  20. Given Expect

  21. Given Expect Behavior

  22. Given! List of all Users

  23. Given! List of all Users Expect! New list of Users

    whose name starts with J
  24. Given! List of all Users Users With J Names Expect!

    New list of Users whose name starts with J
  25. function users_with_j_names($users) { $filtered = array(); ! // ... do

    some work ... return $filtered; }
  26. function users_with_j_names($users) { $filtered = array(); ! // ... do

    some work ... return $filtered; }
  27. 1. Well Named Describes what the function does! not how

    it does it
  28. function users_with_j_names($users) { $filtered = array(); ! // ... do

    some work ... return $filtered; }
  29. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  30. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  31. foreach ($users as $user) { if (0 === strpos($user['name'], 'J'))

    { $filtered[] = $user; } } function users_with_j_names($users) { $filtered = array(); return $filtered; }
  32. Users With J Names Users With K Names Users With

    L Names Users With M Names Users With N Names Users With O Names
  33. Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter
  34. Given! List of all Users One Letter ! Expect! New

    list of Users whose name starts with One Letter Users Name Begins With
  35. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  36. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  37. 2. Change vs Same Separate what changes! from what stays

    the same
  38. 2. Change vs Same Separate what changes! from what stays

    the same Arguments
  39. 2. Change vs Same Separate what changes! from what stays

    the same Behavior Arguments
  40. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  41. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 1. Well Named
  42. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 1. Well Named 2. Change vs Same
  43. 3. One Return Type Return the same value type! given

    any input
  44. Integer! Float! Boolean! String! Array! Object 0 1 2 3!

    0.0 1.0 3.14! true false! "hello" ""! array() array('john', 'eric', 'dan')! DateTime User 3. One Return Type
  45. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  46. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; }
  47. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } ! return $filtered; } 3. One Return Type
  48. Variable Scope http://www.flickr.com/photos/pedrovezini/5747037907

  49. <?php $a = 1; // global scope ! function test()

    { echo $a; // reference to function scope } ! test(); Global Scope
  50. <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  51. <?php $a = 1; // global scope ! function test($b)

    { echo $b; // reference to function scope } ! test($a); Local Variable Scope
  52. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  53. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a);
  54. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1
  55. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1 Avoid using global
  56. <?php $a = 1; // global scope ! function test()

    { global $a; echo $a; // reference to function scope } ! test($a); 1 Avoid using global and its friend $GLOBAL
  57. $_GET! $_POST! $_SERVER! $_FILES Superglobals $_COOKIE! $_SESSION! $_REQUEST! $_ENV

  58. Imagine you're @ http://domain.com/hello.php?name=john <?php ! function test() { echo

    $_GET['name']; } ! test(); Super Global Scope
  59. Imagine you're @ http://domain.com/hello.php?name=john <?php ! function test() { echo

    $_GET['name']; } ! test(); Super Global Scope
  60. Imagine you're @ http://domain.com/hello.php?name=john <?php ! function test() { echo

    $_GET['name']; } ! test(); Super Global Scope john
  61. 4. Control In and Out Control what goes in! and

    what comes out
  62. 4. Control In and Out Control what goes in! and

    what comes out Arguments
  63. 4. Control In and Out Control what goes in! and

    what comes out Return Arguments
  64. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; }
  65. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 4. Control In and Out
  66. function users_name_begins_with($users, $letter) { $filtered = array(); ! foreach ($users

    as $user) { if (0 === strpos($user['name'], $letter)) { $filtered[] = $user; } } return $filtered; } 4. Control In and Out 3. One Return Type 1. Well Named 2. Change vs Same
  67. combine to make Larger Programs Small Programs

  68. None
  69. None
  70. None
  71. None
  72. None
  73. None
  74. BUILD SMALL PROGRAMS

  75. unctions in PHP Part 1: Writing Your Own Functions ƒ