I am getting an error in PHP:
PHP Fatal error: Call to undefined function getCookie
Code:
include('Core/dAmnPHP.php');
$tokenarray = getCookie($username, $password);
Inside of dAmnPHP.php, it includes a function called getCookie inside class dAmnPHP. When I run my script it tells me that the function is undefined.
What am I doing wrong?
It looks like you need to create a new instance of the class before you can use its functions.
Try:
$dAmn = new dAmnPHP;
$dAmn->getCookie($username, $password);
I've not used dAmn before, so I can't be sure, but I pulled my info from here:
https://github.com/DeathShadow/Contra/blob/master/core/dAmnPHP.php
How to reproduce this error:
Put this in a file called a.php:
<?php
include('b.php');
umad();
?>
Put this in a file called b.php:
<?php
class myclass{
function umad(){
print "ok";
}
}
?>
Run it:
PHP Fatal error: Call to undefined function umad() in
/home/el/a.php on line 4
What went wrong:
You can't use methods inside classes without instantiating them first. Do it like this:
<?php
include('b.php');
$mad = new myclass;
$mad->umad();
?>
Then the php interpreter can find the method:
eric#dev ~ $ php a.php
ok
Related
I have a question on the way that functions are declared in PHP.
First test :
File "functions.php" => functions toto(){ return "1"; }
Main File
include("functions.php")
functions toto(){ return "main"; }
echo toto();
Second test
File "functions.php" => functions toto(){ return "1"; }
File "functions2.php" => functions toto(){ return "2"; }
Main File
include("functions.php")
include("functions2.php")
echo toto();
Results
The first test work and echo "main"
The second test doesn't work => fatal error "function toto already define"
I make complementary tests :
in first test : put the functions toto() before include doesn't change the result.
Create twice functions toto() in the main file create Fatal Error
Someone can explain me how exactly this work ?
Thanks for reading
The PHP statements from the include family do not copy-paste the content of the included file in the context of the includer. The inclusion happens at runtime.
In your first example, the function toto() defined in the main file is created during the compilation. Then, on the execution, the functions.php file is read and parsed. It generates an error because it attempts to define the function toto() that is already defined.
The same happens in the second example during the inclusion of functions.php. Also, you get the same error if you declare the function toto() two times in the main script.
Either way, the PHP functions and constants cannot be re-declared.
A quick quote from the documentation:
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
You can check if a function is already defined (to avoid defining it again) by using the function_exists() PHP function:
function toto() { return 1; }
if (! function_exists('toto')) {
function toto() { return 2; }
}
Points to the topic
You can only name one function toto() else you get Fatal error: Cannot redeclare You can use if(!function_exists('toto')){ /*declaration*/ } to prevent that.
php complies a file complete before including next files, means all functions in a php will be declared, then includes are made. So if the first line includes a file that declares toto(), but the next line declares also toto(). The declaration in the include throw the Fatal error.
The only way to prevent this is for example wrap in the first file if(1){ } around the declaration, so know the Fatal Error comes not from the included file.
Test Case:
//file1.php
<?php
function toto(){ echo __FILE__; }
//file2.php
<?php
include 'file1.php';
function toto(){ echo __FILE__; }
toto();
Call:
php file2.php
Result:
Fatal error: Cannot redeclare toto() (previously declared in file2.php)
//file1.php
<?php
function toto(){ echo __FILE__; }
//file2.php
<?php
include 'file1.php';
if(1){
function toto(){ echo __FILE__; }
}
toto();
Call:
php file2.php
Result:
Fatal error: Cannot redeclare toto() (previously declared in file1.php)
I included a file over a template.class.php:
class template {
function loadtemplate() {
include_once("file.tpl");
}
}
after that I created a new object of the class using (in index.php):
$template = new template();
then I executed the method, to include the file (in index.php):
<?php $template->loadtemplate(); ?>
Now I want to execute a function defined in index.php in my file.tpl
error: Undefined variable: var.
How can I pass the variable? If I include the file directly in index.php without using the method, the variable is being passed.
found out a solution by myself:
function loadtemplate($var) {...}
in index.php
<?php $template->loadtemplate($var); ?>
I don't know if I'm asking a stupid question. But I have the following problem when using namespaced function in php 5.6.2.
I'm following this manual page:
http://php.net/manual/en/language.namespaces.importing.php
In the example it says:
// aliasing a function (PHP 5.6+)
use function My\Full\functionName as func;
//some other examples in between;
func(); // calls function My\Full\functionName
So I tried this:
file1.php
<?php
namespace A;
function func() {
return "Hohoho!";
}
?>
index.php
use function A\func as hohoho;
echo hohoho();
PHP gives me the following error:
Fatal error: Call to undefined function A\func()
I am very confused.
Include file1.php inside index.php.
include 'file1.php';
In PHP 4, if you use a class before it's defined you get this error:
Fatal error: Undefined class name
'foo' in...
My code is:
function do_stuff(){
if(foo::what()) ... // this code is before the php file with the foo class is included
}
class foo{
function what(){
...
}
}
do_stuff();
is there any workaround for this (besides telling the people who use your script to update to php5) ?
You could instead use:
call_user_func(array('foo', 'what'));
which would cause the class/method to be checked at runtime rather than compile time.
Define your classes in a file which is require_once()d at the start of your script.
if php4, you can test the existence of a class with class_exists. So to be compatible with php5, you can write this type of code :
<?php
function __autoload($classname) {
include("classes/$classname.class.php");
}
if (!class_exists('foo')) __autoload('foo');
I have a class that looks like this:
utils/Result.php
<?php
class Result
{
public static function ok()
{
echo "OK";
}
}
If I create the following script
./sandbox.php
<?php
require_once("utils/Result.php");
print_r(Result::ok());
And run it with php sandbox.php it works fine. But if I do the following: cd test && php ../sandbox.php it gives me the following error
PHP Fatal error: Call to undefined method Result::ok() in /mnt/hgfs/leapback/sandbox.php on line 5
Now, realize that the require statement seems to be working. If I add a property to the Result class, and use print_r on an instance of it, it looks right. But the static methods disappear. I'm very confused. I'm running php 5.2.6.
Do you have a 'utils/Result.php' file in the directory you have changed to (test)? If yes, it will be included instead of the original file.