Call to undefined function

From Php

Jump to: navigation, search

Contents

Misspelling

This error can show up if you have misspelled the name of a function.

If you have:

 function foo()  // note that this function is not in a class
 {
   echo "hi";
 }

WRONG

 f0o();  // note the zero instead of the O

RIGHT

 foo();

Missing $this->

This error frequently shows up when you forget a $this->.

If you have:

 class Foo
 {
   function foo()
   {
      echo "hi";
   }
   function bar()
   {
      // see below
   }
 }

WRONG

   function bar()
   {
      foo(); // missing $this
   }

RIGHT

   function bar()
   {
      $this->foo();
   }

Missing instance

This error can also indicate that you have to have an instance that the method is called on -- you can't just call the method by itself.

If you have:

 class Foo
 {
   function bar()
   {
      $this->greeting = "hi";
      echo $this->greeting;
   }
 }

WRONG

 bar();  // need to have an instance for the method to work on

RIGHT

 $a = new Foo();
 $a->bar();

Missing class name for static functions

If you have a static function, you need to give the name of the class when you call the function:

If you have:

 class Foo
 {
   static function bar()
   {
      echo "hi";
   }
 }

WRONG

 bar();

RIGHT

 Foo::bar();


NOTE: Static methods in PHP4

PHP5 allows you to define static methods, which allow you to call methods by giving a class name instead of an instance.

PHP4 allows you to use methods as static methods as long as the methods do not refer to a $this variable. For example, if you have:

 class Foo
 {
   function bar()
   {
      $this->greeting = "hi";
      echo $this->greeting;
   }
   function baz()
   {
      echo "hi";
   }
 }

THIS WILL NOT WORK

 Foo::bar();

THIS WILL WORK

 Foo::baz();

You might think this would be kind of stupid (since bar() and baz() do exactly the same thing), and you would be right. Too bad. Computers are not very smart. Computers are dumber than turkeys, and turkeys will drown in the rain.

Personal tools