PHP/Parse.com – Does require() not carry over user "Parse\ParseQuery?" - php

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

Related

Requesting assistance with 1 line of code regarding xajax

I am attempting to build a PHPMailer based email system for a basic website.
This is the line of code that is giving me trouble:
$xajax->printJavascript('xajax/');
Now, this is the tutorial I am using.
Regarding the above line of code, the tutorial says this:
How to use the code inside a webpage?
Place the form (variable), the function (and the includes) before of all html code. Next we need to include some JavaScript file in the documents HTML header (place also php tags):
$xajax->printJavascript('xajax/');
When I run all of the code (including: PHPMailer script; Ajax script), I get this error, on the aforementioned line of code.
Fatal error: Call to a member function on a non-object
So, my question is, do I need to in someway customize this code or make it run to a filepath of some ajax core file or something?
I would be willing to be $xajax is not defined. Try adding this after including the libraries but before including the call to that method:
$xajax = new xajax();
There may be some additional setup required by xajax. A more complete code example could help troubleshoot.

can require_once() be called in the middle of php execution?

I want to write a script that creates a PHP file with some definitions (define(..., ...);) and then within this script i want to run more commands that uses the definitions i just defined in the newly created php file. for this to work i am guessing i could do something like that :
create file (say def.php) and write definitions to it
require_once 'def.php';
run commands that uses definitions from def.php
will this work? if not what is the right way of doing it?
It will work, you can use it but if the file does not exist then it will call exit() in the middle of your page so you will get a half loaded page with an error message.
I think the eval() or anonymous functions (closures) should be better.
http://www.php.net/manual/en/function.eval.php
http://php.net/manual/en/functions.anonymous.php

Open file from a php document, and close it from another?

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.

Accessing external functions from php class

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.

PHP Call to undefined function

I am trying to call a function from another function. I get an error:
Fatal error: Call to undefined function getInitialInformation()
in controller.php on line 24
controller.php file:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
Verified that the require_once works, and the file exists in the specified location.
Verified that the function exists in the file.
I am not able to figure this out. Am I missing something here?
How to reproduce the error, and how to fix it:
Put this code in a file called p.php:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
Solution: use $this->salt(); instead of salt();
So do it like this instead:
<?php
class yoyo{
function salt(){
}
function pepper(){
$this->salt();
}
}
$y = new yoyo();
$y->pepper();
?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
Searched for the php file with the function definition in it. Verified that the file exists.
Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
Verified that the filename is spelled correctly in the require statement.
Echoed a word in the included file, to see if it has been properly included.
Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Many times the problem comes because php does not support short open tags in php.ini file, i.e:
<?
phpinfo();
?>
You must use:
<?php
phpinfo();
?>
Your function is probably in a different namespace than the one you're calling it from.
http://php.net/manual/en/language.namespaces.basics.php
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I include or require_one works on all calls except in a file.
The problem of this file was the code < ?php ? > At the beginning and end of the text.
It was a script that was only < ?, and in that version of apache that was running did not work
This is obviously not the case in this Q,
but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:
I was porting code from JS to PHP and ended up having a class with some public method.
The code that was calling the class (being code that originated from JS) looked something like:
$myObject.method(...)
this is wrong because in PHP it should look like this:
$myObject->method(...)
and it also resulted with "PHP Call to undefined function".
change to use -> and the problem was solved.
Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.

Categories