0

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.

fragadass
  • 65
  • 5

3 Answers3

0

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

ivant87
  • 70
  • 5
  • My calls are in a main script, not in functions. If I create other functions, it will be hardier to maintain. – fragadass Oct 04 '16 at 12:43
  • Yes, but any way you have to know from wich function is made the call of your function ? Or I missing something... – ivant87 Oct 04 '16 at 12:48
  • You can use session to keep the functions that you want to compare.. $session['function_name'] = __FUNCTTION__; and this code put it in all function that you want to use for your function. After that add one more parameter to your function to check if has a session or not, and what is the name of the session (name of the function ) if you know what I mean ? – ivant87 Oct 04 '16 at 12:52
  • No, I want to know if the function is called with a = before (look at the 2 calls in my question) – fragadass Oct 04 '16 at 12:52
  • What a heavy code ! And you still use a second parameter :) I can just use the second parameter if I want (see @some1special's answer) – fragadass Oct 04 '16 at 12:59
  • Okay, remove the parametter and check only the session inside the function. I was wrong for that, it's a session any way :) Just in your main function check for the session.. – ivant87 Oct 04 '16 at 13:03
0

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
0

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.

fragadass
  • 65
  • 5