0

I want to extract function args from the calling function, the issue is that sometimes it can be 3 args and sometimes four.

How I can extract args to variables without getting an undefined warning or similar?

If there is a way the undefined variable can be null or false it's ok too.

Here is an example.

Notice: This is just an example in my case I can't just pass the variables.

<?php

function sendMail( $to, $subject, $body, $headers ) {
    sendSmtp();
}

function sendSmtp() {
    $backtrace = debug_backtrace();

    // This will cause a notify because $attachment is not defined.
    list( $to, $subject, $body, $headers, $attachment ) = $backtrace[0]['args'];
}
Yehuda
  • 157
  • 12
  • 1
    FWIW, using this to pass arguments in the first place is… terrible. – deceze Nov 20 '19 at 09:13
  • I fully agree with deceze here, it's a terrible way of doing things. Consider restructuring your code. – Andrei Nov 20 '19 at 09:14
  • Guys, did you read the notice? I can't! – Yehuda Nov 20 '19 at 09:17
  • @deceze, sorry I can't see how the duplicate related to my question, the "list" is not good for my case. – Yehuda Nov 20 '19 at 09:19
  • 1
    Even as an example, it simply must be pointed out to any future reader that this is not something you should be doing. Maybe your real code uses it for something else, which may be legitimate… but this particular code should never be written by anyone. – deceze Nov 20 '19 at 09:19
  • Huh? I fail to see how the duplicate does *not* answer your question. What exactly doesn't work for you [there](https://stackoverflow.com/a/15365504/476)? – deceze Nov 20 '19 at 09:20
  • See the comment above this line in my example: list( $to, $subject, $body, $headers, $attachment ) = $backtrace[0]['args']; I'm looking for a way to bypass notices of undefined variables. – Yehuda Nov 20 '19 at 09:21
  • 1
    Yeah, here's how to do it: https://stackoverflow.com/a/15365504/476. You pad the args array so you have enough values to unpack into your `list` of variables. – deceze Nov 20 '19 at 09:22
  • I just learned to better read and more carefully, many thanks! – Yehuda Nov 20 '19 at 09:28

0 Answers0