0

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();
3
  • It just ends the function, Nothing is returned, the function is just exited. Commented May 12, 2016 at 9:17
  • 1
    Normally, using return will skip the execution of the rest of the function. Here, as the last statement in a function, it really does not make any sense, since the function would return anyway. Commented May 12, 2016 at 9:19
  • 1
    It means: END THE EXECUTION OF THE OF CURRENT METHOD/FUNCTION BUT NOT THE PROGRAM ITSELF. In other words; jump out of the Method at this point and keep your mouth shut! Don't let no one know what you just did... Just like go back to whosoever sent you but never say a word to him or else you are fired! Commented May 12, 2016 at 9:24

3 Answers 3

3

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__);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

This code makes Perfect sense... 1.) You can see that the name of the Class is Trying 2.) The OP is only curious as to the meaning of the return keyword in the Method. Sure, the code does not do anything reasonable because it was not designed to do anything or make practical, applicable sense but to understand a concept... ;-)
It does not make sense (for me) because there is no context where the code will do something different, or it will return anything. That piece of code is always returning the same, it doesn't matter if the condition is acomplished or not. In other words: it's only a CPU waste. As it is, the only thing that could change it is to override the method in a derived class.
Relax! You are just missing the Point unless you talked, walked, ran and paid your house rent the same day you were born. The Dude is learning and the Question is as relevant as the highest rated Question in SO. But you are right: (FOR YOU) it is pointless... yet not for others who got the point of the question (hence the code). And always remember this: A GREAT MASTER IS ALWAYS THE BEST STUDENT OF HIS OWN SELF. The OP is learning, not really coding. Again look at the name of the Class [Trying] & visualize the context if you will
0

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';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.