Does PHP parse entire files or only on demand? - php

If I write an entire website in a single App class PHP file and include it in every page, then from each page, I call only the related functions and render the page (separate template files) from the App class.
Does PHP actually read the entire script or does it just try to search for only the functions being called? This is with regards to large apps, load times and bandwidth.

PHP reads, compiles, and executes the entire script. This is the only reliable way for it to know where all the functions begin and end.

Quoting the documentation:
"When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward."

If you were to write the entire app in a single file and in a single class it would surely be parsed. PHP would otherwise not know the structure of your class. However, once parsed PHP can be set to store the parsed files so a complete re-parse does not have to be done next time.
As answer to the second part of your question. PHP parses the entire contents of included files. The only distinction is text outside of the tags which does not have to be tokenized etc.

Related

How to get the final php code to be executed replacing all the require and include with their code

First let me apologize if this question was answered before, I just don't seem to find the proper terms to search for it.
The context
I'm viewing some complex PHP code with a lot of require in it, it is kind of ineffective and time consuming to search for a function's or variable's definition through all the multiple level tree of files included in the current file.
The desired result :
so I'm wondering if a function exists that does the following:
Remove all those requires
Replace them with their code recursively
Output it on the browser or return it to a variable
In a nutshell, a function that prints the final PHP code to be executed.
The question :
Is there such a function? if yes, what is its name?
Thank you for the help.
You cannot get the code which is going to be executed because inclusion of code can be done at run-time, based on variables whose values you don't know before running the code.
What you can do is archive all the possible code to be executed, and pack it up as a phar archive.
Using modern tools, that could mean using composer archive.
More information available here https://getcomposer.org/doc/03-cli.md#archive
Besides, it would go against the ecosystem: in modern PHP applications, you use autoloading. Which means that even in projects with millions of lines of code and hundred of thousands of files, there is only one include/require statement.

Redirection on direct access to PHP script

I'm building a website that contains a considerable amount of object-oriented PHP code. To keep the code clean, each class is stored in a single file named [classname].class.php and require_once'd in the script file. Being a form evaluation script, it already has redirections based on the POST variable to prevent dummy execution and database errors.
How do I make it so anyone trying to access the .class.php files gets redirected to the related HTML page, but keep it usable by include and require?
you can either
put it outside the public_html folder
deny access/redirect using filename patterns in the .htaccess
build a small php code in (the top of every class file) that looks for a variable which is initialised in the index. If it's not there, redirect.
If you need further explanation on any of these, just ask.

Scanning a php file for variables

I have a set of templates that contain php variables and logic. I want to find a particular variable, $template, which is an array that is stored at the top of each file. I want to be able to scan, find and then use the variables within this page.
The process doesn't need to be particularly speedy as it's a process that's going to be run to rescan the template for custom fields.
Anyone got any ideas about how this can be achieved?
I cannot simply include it as that will run the logic contained within the php file itself.

How to run external php files and put their output into a string?

Have a abstract how-to question which I haven't found a solution.
Lets say you built a plugin for a CMS like wordpress, I'm using a MCMS called GetSimple.
And now within that plugin.. when a button is clicked by the user... two external php files have their code ran and their output taken and put collectively into a single static file.. say a html or css file.
So then in this kind of scenario... how can you (within the plugin) run an external php file without effecting the current page you are on, then take that output and put it as a string into a variable.. repeat this for another php file... then take the two string outputs, merge them... then put them into a single static file?
This has proven to me to be a very difficult task.
for more details you can see this question:
https://stackoverflow.com/questions/15080163/how-to-create-a-file-with-php
So how would you go about doing this?
I was looking at the possibility of saving each file's output into separate xml files and then merging those xml files... but the problem still remains of running external php and putting that data somewhere without affecting the current page PHP you are on.
If you can generate the response on the server-side, you could simply run those PHP scripts using for example shell_exec() or using Symfony2 Process Component, then gather the results using file() or file_get_contents() functions.
If you need to have this things generated on button click, you must notify server to handle that tasks, and to do so you need to make an AJAX calls calling that scripts using methods I've told you above.

Stop PHP parsing but output the rest of the file

I'm developing a web application where an html page is created for the user. The page could include anything that the user puts in it. We take these pages and add a little PHP at the top to check some things before outputting the actual html. It would look kind of like this:
<?php
require 'checksomestuff.php';
// User's html below
?>
<html>
<!-- user's html -->
</html>
Is there a way to stop PHP from parsing anything after my require? I need the html to be outputted, but, since the user can add anything they want to the html, I don't want any user-added PHP to be executed. Obviously that would be a security issue. So, I want the user's html to be outputted, but not parse any PHP. I would rather not have to put the user's html into another file.
One sensible way would be to offload the user created content to another file and then you should load this file (in your main php file) and output it as is - without parsing it as PHP.
There are many other ways to do this but if creating another file does the job for you then thats probably the best way forward.
UPDATE: Really must read last line of question!
You could encode the html into a variable using base64 encoding which you then just print out the decoded string.
If you don't store the file data in a php file, say n a txt or html file, the php won't be evaluated.
Alternatively you could read the file via file_get_contents() or by some other means which doesn't involve evaluating php.
Though I'm still tempted to ask why you want to do this (particularly this way), it sounds to me like one of the only things that can help you is the special __halt_compiler() function...
That should prevent it from executing the rest of the page, and may or may not output the rest of it. If not, well, read the first (and currently only) example in the PHP's manual for that function (linked above) for how to do it manually.
The only trouble I see with this method is that you'd probably have to have that code in every file you want to do this for, after your require.

Categories