{"id":1198,"date":"2024-05-29T21:26:41","date_gmt":"2024-05-29T19:26:41","guid":{"rendered":"https:\/\/wypo.io\/rendre-une-fonction-php-obsolete\/"},"modified":"2024-06-18T08:18:59","modified_gmt":"2024-06-18T06:18:59","slug":"deprecating-php-function","status":"publish","type":"post","link":"https:\/\/wypo.io\/en\/deprecating-php-function\/","title":{"rendered":"Deprecating a PHP function"},"content":{"rendered":"\n<p class=\" hf_animated fade_bottom default \">To deprecate a function in WordPress, you can use the <code>trigger_error<\/code> function to issue a warning and inform developers that the function should no longer be used. In addition, you can provide a recommended alternative and indicate the version from which the function is deprecated.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-steps-to-make-a-function-deprecated\">Steps to make a function deprecated<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\"><strong>Add depreciation warning<\/strong>: Use <code>trigger_error<\/code> to generate a warning when an deprecated function is called.<\/li>\n\n\n\n<li class=\"\"><strong>Specify alternative<\/strong>: Provide a suggestion for an alternative function or updated method.<\/li>\n\n\n\n<li class=\"\"><strong>Documentation<\/strong>: Document in the code the version from which the function is deprecated and why.<\/li>\n<\/ol>\n\n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-example\">Example<\/h2>\n\n\n\n<p class=\"\">Suppose you have a function called <code>my_old_function<\/code> that you want to make deprecated.<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-428157915\"\n        id=\"codemirror-428157915\"\n      >&lt;?php\nif (!function_exists(&#039;my_old_function&#039;)) {\n    \/**\n     * My Old Function\n     *\n     * @deprecated 1.0.0 Use my_new_function() instead.\n     *\/\n    function my_old_function() {\n        \/\/ Get the backtrace\n        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);\n        $caller = isset($backtrace&#91;1&#93;&#91;&#039;function&#039;&#93;) ? $backtrace&#91;1&#93;&#91;&#039;function&#039;&#93; : &#039;global scope&#039;;\n        $file = isset($backtrace&#91;0&#93;&#91;&#039;file&#039;&#93;) ? $backtrace&#91;0&#93;&#91;&#039;file&#039;&#93; : &#039;unknown file&#039;;\n        $line = isset($backtrace&#91;0&#93;&#91;&#039;line&#039;&#93;) ? $backtrace&#91;0&#93;&#91;&#039;line&#039;&#93; : &#039;unknown line&#039;;\n\n        \/\/ Generate a deprecated notice with caller information\n        trigger_error(\n            sprintf(\n                __(&#039;%1$s is deprecated since version %2$s! Use %3$s instead. Called by %4$s in %5$s on line %6$s.&#039;, &#039;text-domain&#039;),\n                &#039;my_old_function()&#039;,\n                &#039;1.0.0&#039;,\n                &#039;my_new_function()&#039;,\n                $caller,\n                $file,\n                $line\n            ),\n            E_USER_DEPRECATED\n        );\n\n        \/\/ Call the new function as a fallback\n        my_new_function();\n    }\n}\n\n\/**\n * My New Function\n *\n * This is the new function that should be used instead of my_old_function.\n *\/\nfunction my_new_function() {\n    \/\/ Code de la nouvelle fonction\n}\n?&gt;<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-428157915'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-explanation\">Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><code><strong>if (!function_exists('my_old_function')) { \u2026 }<\/strong><\/code>: This ensures that the function is only defined if it hasn&#8217;t already been defined, which is useful for avoiding redefinitions when using hooks or multiple themes\/plugins.<\/li>\n\n\n\n<li class=\"\"><strong><code>trigger_error<\/code><\/strong>: This function generates an <code>E_USER_DEPRECATED<\/code> error, indicating that the function is obsolete. The message includes the name of the obsolete function, the version from which it is obsolete, and the recommended replacement function.<\/li>\n\n\n\n<li class=\"\"><strong><code>my_new_function()<\/code><\/strong>: This function represents the new recommended method that developers should use instead of the old one.<\/li>\n\n\n\n<li class=\"\"><strong><code>debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)<\/code><\/strong>: This function retrieves the call stack, ignoring function arguments to simplify results. The second argument 2 limits the depth of the trace to two levels: the current level and the direct caller.<\/li>\n\n\n\n<li class=\"\"><strong><code>$caller<\/code><\/strong>: This variable contains the name of the function that called the obsolete function. If no function was found (for example, if the call was made from the global namespace), it returns global scope.<\/li>\n\n\n\n<li class=\"\"><strong><code>$file<\/code><\/strong> and <strong><code>$line<\/code><\/strong>: These variables contain the file and line where the obsolete function was called.<\/li>\n<\/ul>\n\n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-use-of-a-hook-to-handle-deprecated-errors-optional\">Use of a hook to handle deprecated errors (optional)<\/h2>\n\n\n\n<p class=\"\">To capture and manage these warnings in WordPress administration, you can add a hook to display warning messages:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-114374597\"\n        id=\"codemirror-114374597\"\n      >&lt;?php\nadd_action(&#039;deprecated_function_run&#039;, function ($function, $replacement, $version) {\n    if (WP_DEBUG) {\n        \/\/ Affiche un message d&#039;avertissement dans l&#039;administration\n        echo sprintf(\n            &#039;&lt;div class=&quot;notice notice-warning&quot;&gt;&lt;p&gt;%1$s is deprecated since version %2$s! Use %3$s instead.&lt;\/p&gt;&lt;\/div&gt;&#039;,\n            $function,\n            $version,\n            $replacement\n        );\n    }\n}, 10, 3);\n?&gt;<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-114374597'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n","protected":false},"excerpt":{"rendered":"<p>To deprecate a function in WordPress, you can use the trigger_error function to issue a warning and inform developers that the function should no longer be used. In addition, you can provide a recommended alternative and indicate the version from which the function is deprecated.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_habefastfse_class":"","hf_wpseo_meta_title":"Rendering a PHP function deprecated [separator]\u00a0[sitetitle]\u00a0","hf_wpseo_meta_description":"D\u00e9couvrez comment rendre une fonction PHP obsol\u00e8te en toute s\u00e9curit\u00e9. Suivez notre guide pratique pour d\u00e9pr\u00e9cier les fonctions PHP, am\u00e9liorer la maintenabilit\u00e9 de votre code.","hf_wpseo_meta_robots_index":"","hf_wpseo_meta_robots_follow":true,"hf_wpseo_meta_robots_advanced":"{}","hf_wpseo_meta_canonical_url":"","footnotes":""},"categories":[28],"tags":[],"class_list":["post-1198","post","type-post","status-publish","format-standard","hentry","category-developer"],"_links":{"self":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/comments?post=1198"}],"version-history":[{"count":2,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1198\/revisions"}],"predecessor-version":[{"id":1472,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1198\/revisions\/1472"}],"wp:attachment":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/media?parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/categories?post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/tags?post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}