PHP variable defined in 'parent' file not recognized in 'required' file

前端 未结 3 830
心在旅途
心在旅途 2021-01-22 22:53

I am trying to access a variable defined in parent file from inside the required file, like this:

<<< parent.php >>>

$controller = \'index\         


        
相关标签:
3条回答
  • 2021-01-22 23:14

    Alright, Got the issue here. It was a mistake of mine, I guess. The error was not in the above lines, but somehow another variable was rather than accessing the parent php, was accessing the child php directly.

    So, guess, it works out now, why the variables were not available.

    0 讨论(0)
  • 2021-01-22 23:15

    Is your variable declared outside a function body? If not, it needs to be.

    Also, when calling the variable from your child file, try "initializing" it with the Global keyword before using it...

    like

    Global $myParentVariable;
    

    ...then try printing it. Also, for debugging purposes, set the variable to a constant in your parent file instead of deriving the value in some function. In other words, make sure it's not a function turning the value to null. (I see you are doing that in the post - but not sure if you modified it for simplicity)

    0 讨论(0)
  • 2021-01-22 23:26

    That is strange. Try to see if variable is part of defined variables:

    $vars = get_defined_vars();
    print_r($vars);
    

    Also, If your variable exists in a function, you can globalize it with global keyword.

    0 讨论(0)
提交回复
热议问题