r/PHPhelp • • Jan 22 '26
How can you access variables in nested functions?
function outer() {
$variable_1 = true;
// [insert an arbitrary amount of additional unrelated variables]
function inner() {
// I need all the variables accessible in here.
}
}I can only think of 2 joke options:
- Painstakingly pass every variable into inner as a discrete argument
- Convert inner into a closure and painstakingly rewrite every variable after
use
Both of those options seem extremely unwieldy and prone to neglect, so surely there’s an actual option out there.
4
Upvotes
15
u/colshrapnel Jan 22 '26
Of course there is. NEVER do nested functions.
This question is infamous XY problem. Tell us why do you think you need nested functions and why the nested one needs all the variables, and we will tell you how it's done normally.
5
u/PhilippStracker Jan 22 '26
It looks like you try to use nested functions as a replacement for a class.
As you already found out: the current options are
useor add function args to the inner functions (which I would recommend, if you stick with nested functions)Here’s a middle ground: Collect all arguments in an object or array, like
But the modern way is to ditch nested functions (they are uncommon and the nesting does makes them difficult to understand)
What I recommend: turn the outer function into a class, and the inner functions into private methods of that class. The arguments are private properties, ideally defined via the constructor. Much cleaner, very common and better to maintain.
4
u/g105b Jan 22 '26 edited Jan 22 '26
The basic concepts of OOP both inflict this restriction (by design), but also provide a solution to your data structure requirements.
It looks like you're coming from JavaScript, which doesn't have the same scoping or object model, so you're applying a design that isn't appropriate for PHP in terms of your requirements.
I would recommend planning out the dependencies of the system you're building and applying OOP designs to it, and you'll find that the language is perfectly capable at keeping your code readable and maintainable without having to juggle local scope.
Maybe introduce a class responsible for drawing, and extend the class into different draw types, and construct the correct class depending on your logic. Then the constructed object will be able to execute it's draw function from within the calling code, already in the knowledge of what type of drawing needs to occur.
5
u/eurosat7 Jan 22 '26 edited Jan 22 '26
For inner functions it is possible to use the
function usesyntax so b sees $settings from outer scope.BUT:
Gathering them into an array if "it should just work" might be better:
Lookup "by reference" if you want to dry it up by introducing side effects.
If you instead want to have it fancier and if you are open to oop you could use a class. Often called a dto:
Hth
2
u/the-fluent-developer Jan 22 '26
Why would you want to nest functions in the first place?
1
u/mossy2100 Jan 22 '26
If you use an arrow (fn) function, all the local vars are passed in automatically.
1
u/Timely-Tale4769 Jan 22 '26
I didn't thought like that. Weird things are most encouragable. Nice
1
u/Terrible_Children Jan 22 '26
By using JavaScript.
Kind of shocked this is apparently so complicated to do in PHP, honestly.
1
u/pabaczek Jan 25 '26 edited Jan 25 '26
Of course this problem is simple if you'd like to learn OOP
1
u/One-Arrival-8298 Jan 25 '26
PHP doesn't support nested functions as you have written it. Function names get defined in global scope. Calling outer more than once will cause a runtime error redefining inner.
0
u/[deleted] Jan 22 '26
[deleted] — view deleted comment