Reputation: 310
Sorry before, maybe this is a very very basic question, but i really do not have an idea with this statement. Here the code, Thanks before :D
Class Trying{
public function theFunction(){
if (get_class($this) == __CLASS__) return;
}
}
$try = new Trying();
$try->theFunction();
Upvotes: 0
Views: 3506
Reputation: 995
That code does not make sense. You can use return
as a way to break the execution of the function without returning any value. But in the that you show does not make sense, because is doing exactly the same always. It does not matter if the condition is true
or false
.
The only way that it can have some sense if you use the class as a base class for another one, and you override that method in the derived class.
Upvotes: 1
Reputation: 6539
You need to return some value. you can either send some data or simple true or false.
You can also return some condition which will return true or false
Class Trying{
public function theFunction(){
return get_class($this) == __CLASS__;
}
}
$try = new Trying();
if($try->theFunction()){
echo 'true';
}else{
echo 'false';
}
Upvotes: 0
Reputation: 14882
When a function is called you are asking the function to do something and return
the result. When a function ends, it will return
null unless told otherwise.
What your function is doing:
{
Am I this class? Return null;
Return null; //end of function. Does this automatically.
}
To be useful the return value needs to be specified, e.g.
{
Am I this class? return true;
Otherwise, return false;
}
The value of this return
will then be the answer (true
or false
).
Starting with your code:
public function theFunction(){
if (get_class($this) == __CLASS__) return;
}
becomes:
public function theFunction(){
if (get_class($this) == __CLASS__) {
return true;
}
return false;
}
which can be refactored into:
/**
* Am I this class?
* @return Boolean
*/
public function theFunction(){
return (get_class($this) == __CLASS__);
}
Upvotes: 3