tags:

views:

117

answers:

3

In some classes I see a call to a function is like:

$this->ClearError();

When the function is residing in that class itself. How is the above approach different from a direct function call like:

return ClearError();
+2  A: 

$this->ClearError();

Refers to the Function inside the Class.

return ClearError()

Calls the function which you defined outside the class of defined seperatly.

Class Demo {
  function _construct() {
   $this -> ClearError(); // refers function inside the class
  }

 function ClearError() {
  return ClearError(); // refers outside the classs
 }
}

function ClearError() {
  contents
}
RSK
+8  A: 

In PHP (unlike C++, for example), you need to use $this->ClearError() in order to call a method on the class. ClearError() calls the global function ClearError().

notJim
notJim
i use this.method() in Java (actually this.property way more) because it lets me very easily not mix up class and local properties.
seanmonstar
A: 

See sathish's answer - the reason for having methods in objects instead of just using functions, is that it allows a set of data to be bundled together, which makes referencing the particular data item a lot clearer.

C.

symcbean
I think "Encapsulation" was the term you were looking for ;)
Paolo
No, thats what I was /describing/ to someone who doesn't know a lot about OO
symcbean