How to Allow Override in PHP

When you plan on using a built-in PHP function repeatedly but want to change how the function works, you can override the function and provide a different implementation. A common reason to override functions is when dealing with a variety of computer systems with different architectures or software versions. Instead of changing code in multiple spots across the script, a few lines enabling an override function can be turned on.

Instructions

    • 1

      Open your PHP source file in a text editor such as Windows Notepad.

    • 2

      Declare a function to override a built-in function by adding the code "override_function('strlen', '$string', 'return override_mystrlen($string);');." The first argument, "strlen," is the function to be overridden. The second argument, "$string," is a list of arguments sent to the new function, separated by a comma. The third argument is a string that provides the new function code.

    • 3

      Add the new override function by adding the code "function override_mystrlen($string) { return (1); }".

    • 4

      Call the new override function by adding the code "$len = strlen('abcd');". Using the previous example, this will return the value 1.

    • 5

      Save the PHP file and load it on your server.

Tips & Warnings

  • PHP tags must be contained inside "<?php" and "?>" tags.

Related Searches:

References

Comments

Related Ads

Featured