Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method of a class?

Tags:

oop

php

I got 2x public functions in a class that must call 1 private function with different parameters also in the same class... for some reason it tell me that it can't find the function...

example:

class Foo {    

private function Do(...)     
{
       ....
       return $whatever;
}

public function One(...)    
{
  return Do(...);
}

public function Two(...)    
{ 
       return Do(...);
} 

}

am getting error:

Fatal error: Call to undefined function do() in ...

like image 340
Lennie Avatar asked Oct 29 '10 20:10

Lennie


1 Answers

You have to use $this to refer to the instance and the T_OBJECT_OPERATOR to access/mutate/call members/methods of an instance, e.g.

$this->do();

Please go through the

  • Chapter on Classes and Objects in the PHP Manual and
  • What is the point of having $this and self:: in PHP?
like image 53
Gordon Avatar answered Oct 23 '22 08:10

Gordon