How to determine if function is Checked from IF

Go To StackoverFlow.com

0

While working in a project written by some one else ,there's a Function which retrieves some Data's and it's return is the Array with Data.

function getSomedata()
{
     //Get some data
     return Array();
}

And on the code this function is tested in many Places as

if(getSomedata())
  getSomedata();

Without editing every piece of code ,can i get notified from php if that Function is called from an IF Statement and return a Boolean instead of the Array with Data.

2012-05-28 08:05
by Burimi
Even if you find a way to do this, you should not do it. It will make your code so much more difficult to debug, and will give a headache to anyone trying to read your code. Instead why don't you assign the result of getSomedata() to a variable, then check if that variable is empty - ziad-saab 2012-05-28 08:08


3

Theoretically, you could use backtracing to figure out which code called a piece of code and read and introspect the code to figure out if the function was called in an if statement. But I'm not even going to go into the details of how to do this, because it's utter madness.

if (function()) function() is an anti-pattern, broken code, bad, bad, bad. You need to fix this misuse instead of fighting it with more madness. If that's used in a lot of places, bite the bullet and fix it once instead of having to deal with it forever.

2012-05-28 08:14
by deceze


-1

I think the best thing you can do is just to use $GLOBALS to check if the Array is set:

function getSomedata()
{
    if(!empty($GLOBALS['Arr']))
        return $Arr;

    //Get some data

    return $Arr;

}

Something like that... It will work for you if data is not changing during script execution. But I suggest you to fix your code.

2012-05-28 08:11
by Roman Newaza
Ads