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();
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();
$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
}
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()
.
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.