0

Two files are dependent on each other:

FILE1:

$var1 = 'Straw ' . $var2;

FILE2:

$var2 = ' berry';
$var3 = $var1;

FILE3:

// this file should include FILE1 and FILE2

How should one go about including FILE1 and FILE2 in FILE3 in a way that makes sure the three variables are properly populated?

Thank you.

camursm
  • 63
  • 1
  • 8
  • 3
    That's a circular dependency. That's simply poor design. If two files are that dependent on each other, have them in the same file. – M. Eriksson Aug 15 '17 at 05:35

3 Answers3

1

Since FILE 1 contains variable used in FILE 2, FILE 1 should be included first in FILE 3. So FILE 3 would be something like

include ("file1.php");
include ("file2.php");

But since the variables are dependent on each other, why not have them in the same file unless you have a reason some how.

NOTE: var2 would be undefined in FILE 1. So my advice is to put both variables that are dependent on each other in one FILE. So FILE 1 an be something like

$var2 = ' berry';
$var1 = 'Straw ' . $var2;

Then FILE 2

$var3 = $var1;

Then you include as above.

Oke Tega
  • 850
  • 10
  • 21
1

You actually will get a warning about an undefined variable regardless of the order you include them due to your design — since file 1 requires $var2 and file 2 requires $var1.

Instead do something like this:

$var2 = 'berry';
$var1 = 'Straw ' . $var2;
$var3 = $var1;

You can split these up in to separate files so long as they are called in the above order.

texelate
  • 2,460
  • 3
  • 24
  • 32
  • Thanks @texalate. This could be one solution. In this example, I wouldn't be able to control the order of the variables however. – camursm Aug 15 '17 at 06:01
0

Many thanks everyone for your input.

One can do as @texelate and @Oke Tega suggests and put the variables in the right order, then distribute them into different files.

But as @Magnus Eriksson pointed out, this is circular dependency and bad design, it simply cannot be done.

camursm
  • 63
  • 1
  • 8
  • 1
    If @texelate's solution solves your issue, then accept that answer instead of writing your own... – M. Eriksson Aug 15 '17 at 05:53
  • The problem is, I wouldn't be able to control the order of the variables, hence this post. – camursm Aug 15 '17 at 05:54
  • Glad we could help but this is not an answer to your question and shouldn't have been posted as an answer. You could comment this. Accept one answer if it helps instead. – Oke Tega Aug 15 '17 at 05:55
  • If you can't control the order of the variables, then you _really_ need to refactor your code from scratch. – M. Eriksson Aug 15 '17 at 05:56
  • Yes sir, that is the plan – camursm Aug 15 '17 at 05:57
  • @Oke Tega Thank you mate for the help. I see now that I should have comment the answers instead, apologize for this – camursm Aug 15 '17 at 05:58
  • 1
    Try refactoring your code from scartch if you can't control the order of your variables.Your code as is would cost a lot later in future if not fixed now. – Oke Tega Aug 15 '17 at 05:58