Advertisement
Shaun_B

Proposed deprecation strategy PHP

Feb 2nd, 2019
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Dummy file logger
  5.  *
  6.  * @param array | string $message
  7.  */
  8. function writeToLogFile($message = null): void
  9. {
  10.     echo print_r($message, TRUE);
  11. }
  12.  
  13. /**
  14.  * Our example method to depreacte
  15.  *
  16.  * @param string $name
  17.  * @deprecated 02 Feb 2019
  18.  */
  19. function capitaliseNames($name)
  20. {
  21.     trigger_error(__METHOD__ . '() is deprecated, use capitaliseWords() instead', E_USER_DEPRECATED);
  22.     ## and / or we'll write to our log file
  23.     writeToLogFile(['deprecated' => 'Deprecated method or function called ' . __METHOD__ . '()']);
  24.    
  25.     ## If we are confident our replacement method or function
  26.     ## is like for like, we can call it here, i.e.,
  27.     ## return capitaliseWords($name);
  28.     ##
  29.     ## If we are still developing the replacement method or function
  30.     ## to what we're deprecating then allow the rest of this to run
  31.     ## so we don't get unexpected results
  32.  
  33.     $name = trim($name);
  34.     $name = explode(' ', $name);
  35.     $returnName = '';
  36.  
  37.     foreach($name as $namePart){
  38.         $returnName .= ucfirst($namePart) . ' ';
  39.     }
  40.     return trim($returnName);
  41. }
  42.  
  43. /**
  44.  * Our example new method, returns
  45.  * capitalisation after each space
  46.  * and removes any whitespaces
  47.  *
  48.  * @param string $name
  49.  * @return string
  50.  */
  51. function capitaliseWords(string $words = null): string
  52. {
  53.     return ucwords(trim($words));
  54. }
  55.  
  56. $name = 'john ian smith';
  57. echo capitaliseNames($name);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement