Using trigger_error to deprecate old PHP systems

If you spend enough time writing code, you'll eventually come across a better way to do something and have the desire to replace all of your previous code with this superior method. Or, you'll find a block of code that you can quickly see is useless or redundant and you want to get rid of it. In both of these scenarios, simply removing the old code and putting in the new code seems like the obvious answer, but it's not always so easy.

So how do you transition old systems in favor of new systems? You deprecate them! It's a very simple process, but one that I only recently began to adopt and am amazed that it took me 5 years of professional development to begin to use. When you deprecate a function, you are just indicating that it should no longer be used, maybe with some additional notes on what is preferred.

To deprecate a function in PHP, I have been using the trigger_error function, which is built into PHP. The function takes 2 parameters - an error message and a severity, ranging from notice to error. When deprecating, I just have trigger_error throw a simple notice saying that this function is deprecated, but then allow processing to continue uninterrupted.

This set up hinges on three basic assumptions that should already be true of your work environment:

  1. Your production machine and work environment are not the same computer. This really should go without saying, but I'm saying it here because I think at one point, a lot of us did this. It's not okay for a lot of reasons, and this trigger_error deprecation is one of them.
  2. Your work environment has error reporting turned to max, and you make a very good effort to remove all errors. This means you run isset() before accessing unknown variables, you initialize arrays before pushing items onto them, and all of those other little errors that are so easy to just pretend don't exist. If you are used to seeing errors, or suppress them, this method is not as reliable.
  3. Your production environment has error reporting turned off. Regular site users shouldn't be seeing any errors, even if they happen. This deprecation method can create a large number of errors, and you don't want your users seeing all of them.

Now that we've discussed what kind of work environment supports this, let's talk a bit about what it is that is being done.

The purpose behind deprecation is to phase out a function and replace it with a similar, but not identical function. If the new function was identical, then you could turn the old function into a wrapper, or even just refactor it. Deprecation works by alerting a developer that they are using a function that is being phased out, and they need to update the code to support the new function.

As an example, I recently phased out one set of models for another. Both models serve similar purposes - providing an object that represents a row in the database. However, for reasons that are beyond the scope of this article, the new models were simply better and I wanted to remove all instances of the old models. I couldn't just delete the old models and hope for the best, because these old models were used in a lot of places.

Instead, I made a very simple modification to the __construct function of all of the old models:

class Lot {
  public function __construct($lot_id=null)  {
    trigger_error('lot class has been deprecated.', E_USER_NOTICE);
 
    if($lot_id) {
      $this->lot_id = $lot_id;
      $this->init();
    }
  }
}

All I did was provide notification to replace this instance of the old model with the new model. Since errors are surpressed on the production server, it has absolutely no impact on the live server. Since I keep my work environment error free, seeing this error pop up on screen is impossible to not notice. This even works within AJAX calls - on the production server, the error is surpressed and the JSON result is intact; on my work environment, the JSON is corrupted by the error and I'm forced to resolve the issue.

There isn't a whole lot to using trigger_error to deprecate code. It's very simple, but has been a huge help lately. I now have a much greater degree of confidence in making large changes. I'm not forced to make the changes all at once, and I can slowly phase out the old code. After a sufficient period of time, I can go back and remove the old code entirely, secure in the thought that I won't break anything.

Here are some examples of where I have used this method recently, in addition to replacing the models as described above:

  • While working on one model, I noticed it had a static member method to fetch a list of all models. I have written a generic 'fetch_all' into the core, abstract model, and so there is no need for this method to exist. I deprecated it and turned it into a wrapper for the generic Model::fetch_all method.
  • I am changing the permission system from a numeric permission system to a string permission system (inspired by Drupal). I did not like the numeric system, since it made the assumption that permission levels only went up. I replaced it with a string permission system, but was not entirely comfortable just deleting the numeric system. Instead, I deprecated the function within the User model to look up the numeric permission level. The string permission system does not call this function, so I only see notices when an attempt to check numeric permission is made.
  • When I took over this system, it was handling request variables in a way that I found to be risky, obfuscating and unneccessary. Unfortunately, it was also used frequently and critically in many systems. I took each instance of this request handling, bundled it into an included file, and deprecated it.

Comments

<p>Trackback from <a href="http://voicelessjam4041.skyrock.com">http://voicelessjam4041.skyrock.com</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://blackredandgold.weebly.com">http://blackredandgold.weebly.com</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://hottestcelebrity.weebly.com/">http://hottestcelebrity.weebly.com/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://google.com">http://google.com</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="https://www.prbuzz.com/health-a-fitness/116563-the-link-between-fat-loss-and-workout-intensity-is-revealed-at-intenseworkoutnet.html">https://www.prbuzz.com/health-a-fitness/116563-the-link-between-fat-loss-and-workout-intensity-is-revealed-at-intenseworkoutnet.html</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://dustybrxz.blog.de">http://dustybrxz.blog.de</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://pinksecret-shop.weebly.com">http://pinksecret-shop.weebly.com</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.miamiherald.com/2013/07/08/3490338/strax-rejuvenation-adds-new-page.html">http://www.miamiherald.com/2013/07/08/3490338/strax-rejuvenation-adds-new-page.html</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://empirestatecavies.weebly.com/">http://empirestatecavies.weebly.com/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://Johnathanfjtp.Bravesites.com/entries/general/you-may-actually-already-be-in-position-to-begin-rebuilding-your-credit-depending-on-the-chapter-of-bankruptcy-you-filed-for">http://Johnathanfjtp.Bravesites.com/entries/general/you-may-actually-already-be-in-position-to-begin-rebuilding-your-credit-depending-on-the-chapter-of-bankruptcy-you-filed-for</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.i-bow.com/xoops/userinfo.php?uid=5761">http://www.i-bow.com/xoops/userinfo.php?uid=5761</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://zelocare.wordpress.com/2012/10/12/will-biotin-help-your-grow-hair-faster/">http://zelocare.wordpress.com/2012/10/12/will-biotin-help-your-grow-hair-faster/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://storify.com/demonicodo472/hacker-facebook-6">http://storify.com/demonicodo472/hacker-facebook-6</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://darwinsrtn.wordpress.com/2013/09/19/lipo-6-fat-burner-lipo-6-is-based-on-six-key-ingredients-that-help-assist-in-burning-fat-in-the-body/">http://darwinsrtn.wordpress.com/2013/09/19/lipo-6-fat-burner-lipo-6-is-based-on-six-key-ingredients-that-help-assist-in-burning-fat-in-the-body/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://sfy.co/rGeI">http://sfy.co/rGeI</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.nexopia.com/users/jessecly/blog/4-nutrex-is-proud-to-say-that-there-lipo-6-is-one-of-the-fastest-methods-for-burning-fat-out-on-the-market-today">http://www.nexopia.com/users/jessecly/blog/4-nutrex-is-proud-to-say-that-there-lipo-6-is-one-of-the-fastest-methods-for-burning-fat-out-on-the-market-today</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=AUSkE1CWxY4">http://www.youtube.com/watch?v=AUSkE1CWxY4</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.arnoldsumc.org/%E3%81%8A%E5%A5%BD%E3%81%8D%E3%81%AA-footwear-nike-%E3%82%B7%E3%83%A7%E3%83%83%E3%83%97/%E5%93%81%E8%B3%AA%E5%90%88%E6%A0%BC-air-footscape-%E6%96%B0%E7%B4%A0%E6%9D%90%E5%85%A5%E8%8D%B7">http://www.arnoldsumc.org/%E3%81%8A%E5%A5%BD%E3%81%8D%E3%81%AA-footwear-nike-%E3%82%B7%E3%83%A7%E3%83%83%E3%83%97/%E5%93%81%E8%B3%AA%E5%90%88%E6%A0%BC-air-footscape-%E6%96%B0%E7%B4%A0%E6%9D%90%E5%85%A5%E8%8D%B7</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.attreff.com/%E7%89%B9%E4%BE%A1-ugg-%E3%82%AA%E3%83%B3%E3%83%A9%E3%82%A4%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%97">http://www.attreff.com/%E7%89%B9%E4%BE%A1-ugg-%E3%82%AA%E3%83%B3%E3%83%A9%E3%82%A4%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%97</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.anhvaem.org/%E4%BF%A1%E9%A0%BC-%E3%82%A2%E3%83%87%E3%82%A3%E3%83%80%E3%82%B9-%E3%81%9D%E3%81%AE%E4%BB%96%E7%AB%B6%E6%8A%80-%E9%80%9A%E8%B2%A9">http://www.anhvaem.org/%E4%BF%A1%E9%A0%BC-%E3%82%A2%E3%83%87%E3%82%A3%E3%83%80%E3%82%B9-%E3%81%9D%E3%81%AE%E4%BB%96%E7%AB%B6%E6%8A%80-%E9%80%9A%E8%B2%A9</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=c6XI3oAnrXg">http://www.youtube.com/watch?v=c6XI3oAnrXg</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.entertaininghealth.com/obesity-and-the-social-network-effect/">http://www.entertaininghealth.com/obesity-and-the-social-network-effect/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://terrencedxuk.newsvine.com/_news/2013/09/20/20595092-immediate-secrets-for-cpr-an-analysis">http://terrencedxuk.newsvine.com/_news/2013/09/20/20595092-immediate-secrets-for-cpr-an-analysis</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=dW771lvEgbE">http://www.youtube.com/watch?v=dW771lvEgbE</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=pPWEAbuZlOI">http://www.youtube.com/watch?v=pPWEAbuZlOI</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://friendlygraveya82.tumblr.com/post/69506170729/employment-advice-everyone-needs-to-keep-in-mind">http://friendlygraveya82.tumblr.com/post/69506170729/employment-advice-everyone-needs-to-keep-in-mind</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://sustainablesolutionsexpo.org/4/tulum-real-estate-beautiful-natural-setting-celebrated-in-expo.htm">http://sustainablesolutionsexpo.org/4/tulum-real-estate-beautiful-natural-setting-celebrated-in-expo.htm</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.antennas.com/forums/member.php?action=profile&amp;uid=984">http://www.antennas.com/forums/member.php?action=profile&amp;uid=984</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.eteamrealtor.com/%E3%81%8A%E6%B0%97%E3%81%AB%E3%81%84%E3%82%8B-timai-lochi-%E6%96%B0%E4%BD%9C">http://www.eteamrealtor.com/%E3%81%8A%E6%B0%97%E3%81%AB%E3%81%84%E3%82%8B-timai-lochi-%E6%96%B0%E4%BD%9C</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.spam404.com/">http://www.spam404.com/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://regarder.telechargerzs.com/sitemap.xml">http://regarder.telechargerzs.com/sitemap.xml</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.evavanrijn.com/lavenham-%E3%83%A9%E3%83%99%E3%83%B3%E3%83%8F%E3%83%A0-brundon-%E3%83%96%E3%83%A9%E3%83%B3%E3%83%89%E3%83%B3-%E3%83%A9%E3%83%A0%E3%83%84%E3%82%A4%E3%83%BC%E3%83%89.html">http://www.evavanrijn.com/lavenham-%E3%83%A9%E3%83%99%E3%83%B3%E3%83%8F%E3%83%A0-brundon-%E3%83%96%E3%83%A9%E3%83%B3%E3%83%89%E3%83%B3-%E3%83%A9%E3%83%A0%E3%83%84%E3%82%A4%E3%83%BC%E3%83%89.html</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=vACG2Y8toI4">http://www.youtube.com/watch?v=vACG2Y8toI4</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://vasquezwfal.bravejournal.com">http://vasquezwfal.bravejournal.com</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming14.frenchdvdrips.com/mere-et-fils-cam-en-francais-gratuit/">http://streaming14.frenchdvdrips.com/mere-et-fils-cam-en-francais-gratuit/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming17.frenchdvdrips.com/endrendrum-punnagai-le-bonheur-eternel-chapitres-1-et-2-regarder-film-complet-gratuit-en-francais-online/">http://streaming17.frenchdvdrips.com/endrendrum-punnagai-le-bonheur-eternel-chapitres-1-et-2-regarder-film-complet-gratuit-en-francais-online/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming5.frenchdvdrips.com/lovelace-film-telecharger-uptobox/">http://streaming5.frenchdvdrips.com/lovelace-film-telecharger-uptobox/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming10.frenchdvdrips.com/jacky-au-royaume-des-filles-bluray-1080p-3d-en-francais-gratuit/">http://streaming10.frenchdvdrips.com/jacky-au-royaume-des-filles-bluray-1080p-3d-en-francais-gratuit/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming2.frenchdvdrips.com/des-etoiles-film-telecharger-blu-ray/">http://streaming2.frenchdvdrips.com/des-etoiles-film-telecharger-blu-ray/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming6.frenchdvdrips.com/des-etoiles-film-telecharger-vuze/">http://streaming6.frenchdvdrips.com/des-etoiles-film-telecharger-vuze/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming19.frenchdvdrips.com/halam-geldi-regarder-haute-defention/">http://streaming19.frenchdvdrips.com/halam-geldi-regarder-haute-defention/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming15.frenchdvdrips.com/fruitvale-station-entier-streaming/">http://streaming15.frenchdvdrips.com/fruitvale-station-entier-streaming/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming11.frenchdvdrips.com/la-vie-revee-de-walter-mitty-regarder-3d-en-streaming/">http://streaming11.frenchdvdrips.com/la-vie-revee-de-walter-mitty-regarder-3d-en-streaming/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming20.frenchdvdrips.com/labsence-film-online/">http://streaming20.frenchdvdrips.com/labsence-film-online/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming8.frenchdvdrips.com/lulu-femme-nue-film-telecharger-mac/">http://streaming8.frenchdvdrips.com/lulu-femme-nue-film-telecharger-mac/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming13.frenchdvdrips.com/jacky-au-royaume-des-filles-chapitres-1-et-2-film-gratuit/">http://streaming13.frenchdvdrips.com/jacky-au-royaume-des-filles-chapitres-1-et-2-film-gratuit/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming3.frenchdvdrips.com/beaucoup-de-bruit-pour-rien-vostfr/">http://streaming3.frenchdvdrips.com/beaucoup-de-bruit-pour-rien-vostfr/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://streaming1.frenchdvdrips.com/ghost-in-the-shell-arise-border-2-ghost-whispers-telecharger-film-complet/">http://streaming1.frenchdvdrips.com/ghost-in-the-shell-arise-border-2-ghost-whispers-telecharger-film-complet/</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.youtube.com/watch?v=O0sPeM8MaSo">http://www.youtube.com/watch?v=O0sPeM8MaSo</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

<p>Trackback from <a href="http://www.dailymotion.com/video/x1ahprf_minecraft-download-skad-pobrac-sciagnac-minecraft">http://www.dailymotion.com/video/x1ahprf_minecraft-download-skad-pobrac-sciagnac-minecraft</a>.</p><p>Using trigger_error to deprecate old PHP systems | Steve Phillips</p>

Pages