r/PHP • • Sep 18 '26

Article In PHP, Loading a File Means Running It

Can we talk about units in procedural PHP?

I was testing a feature of my debugger on a plain script with a function in the middle of it. I passed [1, 2] as the argument, the breakpoint paused, and the variables panel showed [10, 20, 30].

and then... take a look in: https://ddless.com/blog/in-php-loading-a-file-means-running-it

0 Upvotes

17 comments sorted by

4

u/colshrapnel Sep 18 '26

Wonder if extra post in this sub was caused by same behavior.

-6

u/RequirementWeird5517 Sep 18 '26

I just thought there might be people who didn't know PHP in depth...

3

u/Teszzt Sep 18 '26

What would be your desired behavior instead?

-1

u/RequirementWeird5517 Sep 18 '26

None, really. PHP is doing exactly what it promises... I just wrote down the moment I realized the "bug" in my tool was me not knowing how include works :D

2

u/Aggressive_Ad_5454 Sep 18 '26

True. It’s an interpreted language.

At the same time, including or requiring a .php file parses it into opcodes and stores them into the OPcache.

2

u/TemporarySun314 Sep 18 '26

Even in compiled languages like C you can have side effects in included files. With some trickery you can even achieve that code gets executed when running the program, just from the #include alone.

In general you have to trust any code you somehow include or use in your code. No matter what language

-1

u/RequirementWeird5517 Sep 18 '26

True, but that's only the compile half... Include compiles and then runs. OPcache saves the compile on the next request, never the run, so the script's flow happens every time.

That's why opcache_compile_file() is fun, compile without run :D

9

u/Winter-Paramedic3900 Sep 18 '26

Are you a moron OP? What's the point of this post unless it's targeted to people who have never seen or used PHP in their lives? Obviously the include keyword includes code into the interpreter.

1

u/matthewralston Sep 19 '26

I think this is probably quite well known to people who have been using PHP for a long time, particular before the modern (ish) patterns of OOP, keeping a single class per file, not including unscoped executable code in a class file, etc.

If you've got grey in you're beard like me (just realised I've been using PHP for 26 years 😯) and learned cleaner ways of doing things along the way then it comes as no surprise at all.

I am now mildly curious to see what ddless is now though. Never heard of it before. I assume a debugger. I've always been in the echo, dump, dd, etc. category for my sins. I know, I know.

1

u/RequirementWeird5517 Sep 19 '26

Thanks! DDLess is a PHP workbench step debugging without Xdebug, plus a task runner, method execution, timeframe and a playground. No extension, no ini. Built it for the dump/dd crowd, myself included :)

1

u/goodwill764 Sep 19 '26

Same as old require in nodejs. Same as python.

Only difference is the scope.

0

u/bomphcheese Sep 20 '26

> PHP Has No Import ... There is no mode that says "give me the definitions and skip the rest".

Yes there is. Maybe take some time to learn the language before making such statements.

3

u/schorsch3000 Sep 20 '26

You might try this out. how are you importing a class from another file without including or requiring that file in the first place?

you just googled "php import" didn't read and blamed op, didn't ya?

1

u/bomphcheese Sep 29 '26

Auto loading? This is a long-solved problem.

1

u/schorsch3000 Sep 30 '26

how will autoloading

"give me the definitions and skip the rest" ?

autoloading is just some other code including / requiring the code for me. Its fine, in fact it's wonderfull, but it ist just a fancy way to require/include, it does not skip side effects

1

u/bomphcheese Oct 01 '26

Maybe I just don't understand the problem that is at play. They want all of the class/function definitions from a file, without executing any other code? Why would a file contain free-floating code outside of a class or function ... unless maybe it's a template?

@use basically makes the functions available without actually executing anything. Auto-loading is somewhat similar. Yes it is just the same as including, but both just provide definitions, if they only put definitions in the file, which is standard practice.

I read that blog post, but it seems to me that the answer is obvious... unless I just don't understand the problem.

1

u/schorsch3000 Oct 01 '26

nah, you are absolutely right on this one, there is no real world problem, it's just that op wants to do things the in a way their favorite languages does and refuses to have a file either run code OR have functions / classes.

there is no way to have that in php (other than parsing the file, and do evil eval magic) but thats fine :-D