简体   繁体   中英

Can I know how is called a PHP function?

I'm writing a function that can returns an integer value or write this integer into a file. I want this choice to be done just by the call of the function. Can I do it ?

Here is the function :

function directory_space_used($directory) {
// Space used by the $directory
  ...
  if ( "call #1" ) return $space_used;
  if ( "call #2" ) {
    $file=fopen(path/to/file, 'w');
    fwrite($file, $space_used);
    fclose($file);
  }
  return null;
}

Call #1 :

$hyper_space = directory_space_used('awesome/directory');
echo "$hyper_space bytes used.";

Call #2 :

directory_space_used('awesome/directory'); // Write in file path/to/file

If it's not possible, I can use a 2nd parameter in the function but I want to keep the parameters' number as low as possible.

Thanks.

Yes, you can use this magic constant

__FUNCTION__

and you can read about it here

Just make one more parameter on your function, and this parametter will be the name of the function that the request come from and after that you can use it in the if statement.

this is a pseudo code:

       //function that you want to compare
        function test1() {
        //do stuff here
        $session['function_name'] = __FUNCTTION__;
        directory_space_used($directory,$function_name);
        }

        //Other function that you want to compare
        function test2() {
        //do stuff here
        $session['function_name'] = __FUNCTTION__;
    }

function directory_space_used($directory) {
        // Space used by the $directory
          ...
           if(isset($session['function_name'])) {
          if ('test1' == $function_name ) return $space_used;
          if ( 'test2' == $function_name ) {
            $file=fopen(path/to/file, 'w');
            fwrite($file, $space_used);
            fclose($file);
          }
        } else {
//something else 
}

          return null;
        }

I think will be better option to use switch cases... this is just a note .

the test1 amd test2 can be anywhere in your php files and folders

You could keep a count in a session variable, but i'd suggest a second parameter. It's cleaner to maintain, and you can always set a default value, so that it's only used for one of the cases

function directory_space_used($directory, $tofile = false) {
// Space used by the $directory
...
if ( $tofile )  {
   $file=fopen(path/to/file, 'w');
   fwrite($file, $space_used);
   fclose($file);
}else{
   return $space_used;
}
  return null;
}

And then just call it like:

directory_space_used('....', true) // saves in a file
directory_space_used('....') // return int

Thanks all, it seems the better way is to put a second parameter in the function. Not as funny as I wanted, but it works easily, without using lots of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM