Dealing with function redefinitions php

Dealing with function redefinitions php

include_once and require_once are very useful if you have a library of common functions. If you try to override with – that is define – an identically named local function however, PHP will halt noting that it cannot redeclare functions. You can allow for this by bracketing (within the include file):

function myUsefulFunc($arg1, $arg2) {
     ... }

with
if (!function_exists('myUsefulFunc')) {
function myUsefulFunc($arg1, $arg2) {
     ... }}

Top level functions (ie. those not defined within other functions or dependent on code running) in the local file are always parsed first, so http://php.net/function_exists within the included/required file is safe – it doesn’t matter where the include statements are in the local code.

Thanks to : http://php.net/manual/en/function.include-once.php

also Thanks to : http://smorgasbork.com/component/content/article/34-web/64-autoload-conflicts

Share on Facebook



One thought on “Dealing with function redefinitions php”