PHP Call to undefined function - php

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.

Related

What is _once's true meaning in file manipulation?

I have a doubt that makes me very confused, it's about require_once. I saw a question on StackOverflow about it but I'm still not sure I understand...
I know the difference between require and include is that using require, the script will issue a fatal error and stop execution, while include does not. But there is something that I have been asking myself, the true meaning of _once, most people say something like: PHP will check if the file has already been included, and if so, not include (require) it again. How will the script be required only once? I don't understand this, because in my point of view PHP scripts are not like CSS that our browser downloads and is saved on the machine, so then not needing to download again, I think that every time a PHP script is executed, it will do its job again, there is no way to understand that it has already been required just as browsers understand that we have already downloaded CSS files.
What is the real meaning of _once? And could someone please give me an example, because I don't really understand it, I'm a beginner, I don't even know if I'm asking a proper question
If possible demonstrate some code so that I finally understand
Imagine this scenario. It's nice to be able to include the same file as you can use it multiple times with different outcomes:
display.php
echo $var;
index.php
$var = 'Hello ';
include('display.php');
$var = 'World ';
include('display.php');
This outputs:
Hello
World
However, sometimes this will cause problems. This could be an include in different files, so you need to include_once, but to illustrate:
functions.php
function display($var) { echo $var; }
index.php
include('functions.php');
display('Hello ');
//more code
include('functions.php');
display('World ');
Generates a fatal error:
Fatal error: Cannot redeclare display() (previously declared in index.php:1) in index.php on line 6
So if you have hundreds of files and some include others depending on some logic/program flow but some files can only be included once (as seen above) then that is the use case.

check if functions referenced inside functions exist before commands are processed - PHP

I modified about 600 lines of code amongst over 5000 lines of code by updating function calls to match the new library I created for use with the script. I have spot some errors manually when updating before by hand, but I believe I overlooked some.
So far, the only way I can spot them is to run the code and have it crash when the error happens. This is a bad idea because such errors will happen before resources are freed.
Here's an example in code that explains my question:
Say I have mainline code (called index.php) that consists of this:
<?php
include "library.php";
$file=fopen("afile","w");
doWrite($file);
brokenFunction();
fclose($file);
exit();
?>
and say library.php contains only this:
<?php
function doWrite($file){
fwrite($file,"Test");
doNothing();
}
?>
Because brokenFunction(); and doNothing(); don't exist, an error is expected. Rather than PHP compile then execute code up until the first failing function call, how do I have PHP check to see if all referenced functions the mainline code links to exist before executing code?
So in my example, I expect an error and the code to stop compiling/executing at $file=fopen("afile","w"); because brokenFunction(); and doNothing(); don't exist.
How do I achieve this?
You can use the built-in function_exists() function:
if (!function_exists('brokenFunction')) {
throw new \Exception('brokenFunction is missing');
}
But this will only raise an error when executing the code.
Some tools like PHPStorm can check your code (without running it) and throw warnings if a function is missing.
Some other tools are listed in this (closed) SO question: Is there a static code analyzer [like Lint] for PHP files?.
The best way I've found to globally debug an environment without using #A.L's method and pasting a function_exists call before every edited line, is to use a PHP debugger of some sort, most likely built into an IDE that compares every function call line against a 'test compile' of your code and all included libraries to make sure the called function exists (and would likely underline it in red if it didn't). A PHP IDE like Aptana might be what you're looking for (especially if you see yourself having future updates to run as this solution will have the time overhead of installing/setting up Aptana).

Use of undefined constant notice

I've read up on this problem a little, mainly from articles on here. It appears that it is typically generated by someone trying to do $foo[bar] instead of $foo['bar'], but I have checked several times around where the error is occuring in my script and this is not the case.
I have a php file, which contains the following script:
define("APP_PATH", "http://localhost/foobar");
require_once APP_PATH . "/classes/controller.php";
This appears to be executing fine. Inside controller.php I have this code:
require_once APP_PATH . "/classes/factory.class.php";
$factory = new factory;
This, to my knowledge, should execute perfectly fine. However, I am getting the following error: Notice: Use of undefined constant APP_PATH - assumed 'APP_PATH' in C:\wamp\www\foobar\classes\controller.php on line 3. Line 3 is the call to require_once.
I have checked, I am fairly sure that this should not be causing an error. I've also checked my spelling. The same line also triggers a warning and a fatal error about failing to open the stream, it is returning APP_PATH/classes/factory.class.php as the path.
Any help would be much appreciated.
The problem is that you are including from a remote place.
Let's say that APP_PATH.'/classes/controller.php' is as follows:
<?php
class Some_Controller extends Controller {
// ...
}
echo 'TEST!';
When you include it through HTTP the PHP interpreter will parse the file before sending it back to be included:
<?php
include APP_PATH.'/classes/controller.php';
// This will print "TEST!" to the page because PHP has
// parsed the code and the only thing in the output
// buffer is "TEST!" (from the "echo 'TEST!';")
In order to fix this you need to include from the local environment. In Linux it would be some like
/path/to/web/classes/controller.php
In Windows it would be something like:
C:\path\to\web\classes\controller.php

Call to undefined function from another php file

Alright this is what my code looks like
index.php
require_once($WebsiteRoot . "/include/testfile.php");
TestFunction();
/include/testfile.php
function TestFunction()
{
echo "It Works";
}
And it gives me the error:
Fatal error:
Call to undefined function TestFunction() in /path/index.php on line 49
Any idea what i'm doing wrong?
Thanks
You haven't included a <?php tag in the included file, so it's just interpreted as plaintext input.
Remember... there's no such thing as a PHP script. There's only files which contain PHP code blocks. Without at least one <?php opening tag, the PHP interpreter will never be invoked and the file's contents will simply be treated as output.
try calling another function from testfile.php, if this is'nt working, its something with the include. Add the code:
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
to the top of index.php and refresh the browser to see your errors, try debugging from there.
The problem that i can forsee is that you are using a URL instead of a path, your $websiteRoot variable should contain a path like:
$websiteRoot = "/var/www/html/websiteName";
OR
$websiteRoot = "C://xampp/htdocs/websiteName";
instead of a URL like:
$websiteRoot = "http://www.somesite.com";
I had a similar issue. I dug into the PHP in the included file and found an invalid PHP tag. I had <? instead of <?php. PHP 7.2 and earlier forgave that, but PHP 7.3 was throwing that same error you faced.
Make sure you're including the file you think you are. If your index.php page looks exactly like you've stated, then it won't return anything.
If you want to link to the same location from anywhere on the site without worrying about relative locations, then at the beginning of the file, put:
$WebsiteRoot=$_SERVER['DOCUMENT_ROOT'];
And it should work fine, provided your file would be located at http://mywebsite.com/include/testfile.php
Try renaming the included file.
I had an included file with the name "system.php". It looked as if the include command was just skipped. Even with the most strict error reporting there was no message and even an echo command in the main body of the included file did not produce output. It had worked ok under PHP 5 but after the upgrade to a 7.2 environment these problems arose. After much effort - I forgot how - I managed to get an error message. It said there was a conflict with a PEAR class with the name "system". Yet my file didn't contain any class, just variables and functions. Anyway, giving the file another name than "system.php" worked for me.
I hope someone else can add a more technical comment on what was going wrong here.

What on earth could cause this PHP error? Bug in PHP?

I received the following error:
[27-Apr-2009 10:26:06] PHP Fatal error: Cannot redeclare alphanumeric() (previously declared in /home/iddoc/public_html/lib/common.php:6) in /home/iddoc/public_html/lib/common.php on line 8
Notice this:
/home/iddoc/public_html/lib/common.php:6) in
/home/iddoc/public_html/lib/common.php on line 8
Here are the offending lines:
function alphanumeric($str) {
return strtolower(preg_replace("/[^A-Za-z0-9]/",'',$str));
}
Prior to these lines there are only comments. There is no other declaration of that function anywhere else in that file or any other.
Strange, no?
Are you using require_once() to include common.php everywhere? If you use just require or include, that will cause this issue.
Are you using require/include to reference the file? This is a common error when including a file twice. PHP doesn't know what to do if it sees two declarations, even if they're from an identical file.
Try using this:
include_once('lib/common.php');
It seems like you may have included the common.php file more than once. The second time it loaded would cause errors like that since the function is already loaded.
It is an odd error, but will probably seem logical once the problem is figured out. I would suggest looking at your includes, or switch them to include_once. Maybe see what debug_print_backtrace() outputs before that function loads.
Sounds like you've got the common.php file included more than once. Do you use include(), require() or require_once() for your includes?
is this common.php included earlier? That could cause this error.
You should always use include_once or require_once when including code files in PHP. There are exceptions to this rule but if you aren't sure, use the _once versions. The only time it is okay to use include or require is when you know the file will never, ever be included again in the same program OR the file being included does not declare functions (without protecting them with an if (function_exists()) {} block).
As an example, templating system are one of the very few uses where you probably don't want the _once version.
Check for recursive includes and make sure your blocks are not executed several times.

Categories