Hello let's say that I have the following structure in my application .
<?
include('includes/functions.php');
include('includes/classes/login.class.php');
$login = new login();
?>
What I want is inside the login class to call a function that is defined in functions.php . But I can't get it to work.
PHP does not care in which file you have storred what function or class definition. Only namespaces, order or processing and of course where inside classes or functions you have what definitions matter.
What you are doing is correct.
Including php-code just adds the content of that file in the file where you execute the include. If this is not working there is something else wrong with your code.
You should close this question and make a new one. Include the content of the files you are including and the errors that are displayed.
Related
First, I do not hope this question is too stupid for stackoverflow, but I am quite new to php and do not have much experience.
I have the file page.php and the file sendTestMail.php and I would like to run the file sendTestMail.php with specific parameters.
I am calling sendTestMail.php like that:
require_once WPGAMAIL_PATH.'sendTestMail.php';
And I need to set two array parameters $wp_set and $ga_set.
Any suggestions what a best-practice solution is?
I appreciate your replies!
You have tons of possiblities, however without knowing the structure within your sendTestMail.php I can only give you hints.
You should basically create a function within your sendTestMail.php - e.g.
<?php
function sendTestMail($wp_set, $ga_set) {
/* your actual code ...*/
}
If you now require the script you can simply call the function an pass the parameters
<?php
require_once WPGAMAIL_PATH.'sendTestMail.php';
sendTestMail('john', 'doe'); // or whatever your parameters are
Other examples of how to pass variables to included / required files can be found here
PHP pass variable to include
I want to run code from awesomeQuery.php on multiple pages, so I use PHP require.
<?php require_once("awesomeQuery.php") ?>
The awesomeQuery.php looks something like this:
<?php
require 'vendor/autoload.php';
use Parse\ParseQuery;
[query code here that works]
?>
Now here's spiffyPage.php:
<?php
//This line here works beautifully!
require_once("awesomeQuery.php");
//If I make a new query code in this file I get the following error:
//Fatal error: Class 'ParseQuery' not found in /path/spiffyPage.php on line 45
[some other similar query code]
?>
Is there a reason why this new query isn't working? Didn't I already call use Parse\ParseQuery from awesomeQuery.php? Does it not carry over into spiffyPage.php?
If I call use Parse\ParseQuery separately in spiffyPage.php, then the new query code does work… but I'd rather not have to call it every time!
My guess: All the PHP code in awesomeQuery.php gets executed before being pasted into spiffyPage.php, so the code use Parse\ParseQuery doesn't get pasted into spiffyPage.php. Is this correct? If so, can I paste code into a file from another file without executing the code first?
the PHP manual clearly says that
Note:
Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
http://php.net/manual/en/language.namespaces.importing.php
I am trying to do the following:
Open a file, say "myfile.json" from a php- let's call it "utils.php"; Use it in other php pages; close it from another php.
I have tried to include "utils.php" in the other files and write in the utils file, but it does not seem to work. I suppose this happens because utils.php is never actually executed, only included, but if I should execute it, how can I do it without having to refresh any page, preferably right when the user gets on the main page? This should not be seen by the user, what he sees should remain the main page.
Thanks in advance, I am quite new to php, and am trying to learn.
When you include a file, you are running all code inside it. The functions and classes will not be evaluated but will be defined for future use. If you open your file as this example:
util.php
<?php
$file_hand = fopen('/tmp/file.txt','r');
You will have a handle if the operation is completed. However, the variable $file_hand is global. If you need to use a function to close it, you will need the following code to do it:
other.php
function close_file(){
global $file_hand;
fclose($file_hand)
}
or you can pass the handle as parameter like:
function close_file($file_hand){
fclose($file_hand)
}
Doesn't matter how you will close the file. You have to make sure the variable you are using is the same created in utils.php. If you close like this:
function close_file(){
fclose($file_hand)
}
The variable you've created in until.php file is different of this one.
i am very new to php, i am used to writing .net, and i am finding the includes hard to understand and was hoping someone could help me understand how to correctly use an include once in a file, rather than inside each function..
take the following as an example
<?php
include 'test.php';
function test($a)
{
echo $value_from_test_php;
}
?>
the above code does not seem to work... however the below does
<?php
function test($a)
{
include 'test.php'
echo $value_from_test_php;
}
?>
i am having a hard time figuring out how to make an include work for all functions inside a file, rather then including it inside each function, any advice is greatly appreciated!
It's the scope of variable which is troubling you rather than includes, in PHP generally includes are used where there's a common page/markup to be included on each page, such as footer, header, etc
There are 4 types
include
include_once
require
require_once
The only difference is include will throw you an error if something goes wrong and will continue to execute the script where require will halt the further execution
You'll get everything here on includes - PHP Documentation
It all depends what is inside the file that you are include-ing! I would never, ever, suggest using include inside a function (or loop, or pretty much anything with brackets). Remember, the contents of the file being included are literally just "plopped in" place, right where the include statement is. So whatever scope (global, class, function, etc.) you're in when you include, is the scope that its contents will be declared in.
Put full class and function definitions in files, and include them at the top of the files where they are going to be used.
Your issue is not related to includes, but rather variable scope. By default a variable defined outside a function is not available within the function.
It's difficult to suggest the best solution without knowing exactly what it is you're trying to do, but the documentation (linked above) should get you started.
First example is not working because you use variable from global scope, if you want to use it then replace $value_from_test_php to $GLOBALS['my_var_name']
From what I understand using something like require_once will essentially copy and paste the code from one file into another, as if it was in the first file originally.
Meaning if I was to do something like this it would be valid
foo.php
<?php
require_once("bar.php");
?>
bar.php
<?php
print "Hello World!"
?>
running php foo.php will just output "Hello World!"
Now my question is, if I include require_once inside a method, will the file that is included be loaded when the script is loaded, or only when the method is called?.
And if it is only when the method is called, is there any benefit performance wise. Or would it be the same as if I had kept all the code into one big file.
I'm mainly asking as I've created an API file, which handles a large amount of calls, and I wan't to simplify the file. (I know I can do this just be creating separate classes, but I thought this would be good to know)
(Sorry if this has already been asked, I wasn't sure what to search for)
It will only include when the method is called, but have you looked at autoloading?
1) Only when the method is called.
2) I would imagine there's an intangible benefit to loading on the fly so the PHP interpreter doesn't have to parse extra code if it's not being used.
I usually use the include('bar.php'); i use it for when i use databvase information, i have a file called database.php with login info and when the file loads it calls it right up. I don't need to call up the function. It may not be the most effective and efficient but it works for me. You can also use include_once... include basically does what you want it to, it copies the code essencially..
As others have mentioned, yes, it's included just-in-time.
However, watch out for variable definitions (require()ing from a method will only allow access to local variables in that method's scope).
Keep in mind you can also return values (i.e. strings) from the included file, as well as buffer output with ob_start() etc.