6

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Ben Alter
  • 99
  • 1
  • 1
  • 3
  • 7
    If you are told that the function is undefined, then thats what the problem is. You need to show how and where you create the funtion if you want a better answer. – Repox May 30 '12 at 13:19
  • 6
    you have no warnings of file not found? Try require instead of include to confirm that file is included. – Akshat Goel May 30 '12 at 13:20
  • add this error_reporting(E_ALL); to the top of the script to check if you get an E_WARNING –  May 30 '12 at 13:31
  • You might also want to echo some output inside your included file, to make sure that you're including the file you think you're including. The number of hours I've wasted trying to track that down.... – andrewsi May 30 '12 at 13:34
  • @Repox: The function is clearly defined... it's inside of a class _dAmnPHP_ inside of the file dAmnPHP.php. I don't know if being inside the class is the issue or not. – Ben Alter May 30 '12 at 13:37
  • 2
    Try `dAmnPHP::getCookie($username, $password);` – maxdec May 30 '12 at 13:40
  • It is the issue, if it's inside a class you may first initialize the class $class_name = new dAmnPHP(); then call the function $class_name->getCookie(); or make it static dAmnPHP::getCookie(); –  May 30 '12 at 13:40

2 Answers2

9

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

Maythe
  • 576
  • 4
  • 13
4

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 1
    To use the functions in a class you must instantiate a new class by putting the word new before it, without the parenthesis after, and assign that to a variable as shown in the example at the bottom. – Eric Leschinski Nov 27 '16 at 13:06