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.
Related
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.
I'm new to Joomla, but not a new programmer. I've written several applications in PHP that I want to include inside Joomla articles. Simple enough:
<?php include 'file.php'; ?>
The issue is that inside the PHP files I have a bunch of code gathering and creating variables that I need to POST and retrieve. I can get those POST variables inside the Article, but I can't pass them back to the included PHP file.
I've even coded the included PHP files to access the Joomla framework hoping to retrieve Joomla user id for example. This won't run inside the Article either and returns empty. However, if I run the PHP file on its own outside of the Article, I can access all POST data (obviously) and also the Joomla JFactory data. So it runs fine, until it's placed as an included file inside an Article.
The only way I've been able to pass something to the included PHP file is using $_GET url variables like this:
<?php include 'file.php?data=something'; ?>
However, this simply isn't practical as I have too many variables to pass like this. Normally, included PHP files run as part of the parent script and have access to all variables. How can I accomplish this in Joomla??
Much appreciated!
It is not recommended to include php into articles. Try instead one of these approaches:
Load your code in the index.php - file in your template: /templates/yourtemplate/index.php. This file is called every time your page is called.
Make a template override of the component where you want you external php file to be loaded. If this is in an article, you copy /components/com_content/views/article/tmpl/default.php (and possibly also other files there) to /templates/yourtemplate/html/com_content/article. Then add your include-statement here. (info) This file will be loaded each time you view a single article, but you can have further logic to only run it if (whatever)...
Use Jobin Jose's approach if you want to load you php-file inside content in an article. (info)
Some other approach writing a plugin
...or a component
I would say probably the easiest method is 2. (or 1.), but it all depends what you want to do.
Try this,
You have to create a simple module for your requirement, means what you are trying to achieve with included php file.
then place that module within the article with {loadposition module_position}
then you will get all the POST variables to that article and also suppose user_id and all other joomla Factory can be accessed bcoz its a Joomla module.
for a quick tutorial about module creation can be found here.
hope it make sense.
I resolved this using an Extension called Sourcerer
https://www.nonumber.nl/extensions/sourcerer
Variables can be passed without issue now.
Thanks for the input.
Why do not you try to create a new Joomla component and do whatever you want it it?
I am absolutely beginner in php/mysql but managed to do it with my page, following this instructions:
http://www.inmotionhosting.com/support/edu/joomla-3/create-component/helloworld
When you create this component, whatever you write in its "default.php" file, is actually a blank HTML/PHP page, with your joomla design and some other Joomla-features.
Sorry for the amateur answer and terminology :)
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.
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.
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.