Jump to content

about: return


Recommended Posts


First of all i am very weak about OOP and classes in php. I am learning now but i stuck with very silly problem.

My codes are below.

   class myTestClass
       function __construct()
         $this->OldName("This is Old Name");
       function OldName($VeryOld)
           if($VeryOld == "This is Old Name")
               $VeryOld = "Something Wrong";
            return $VeryOld;
       function NewName()
           echo "This is Brand New Name";
   $i = new myTestClass();


if i send value to  OldName "This is Old Name" then codes works fine.

But if i send "This is Old Nameeee" then does not appear anything.


what i want to do is print "Something Wrong" text if i enter different value. I have no idea what return do and how it do? How to retrieve/print "Something Wrong"?

i can do that with echo but i want to transfer result of return to another function.


Really appreciate for any help/idea.

Link to comment
Share on other sites

Firstly, your logic itself doesn't really make sense. It says, (calling OldName()) if the username provided is the same as the current name, call a function that displays 'this is a brand new name'


Can you see any sense it that logic?

Link to comment
Share on other sites

First of all, i am very very very beginner of class.

All i want to do is autorun class when it created. Then Skip to OldName and parse same variable.

Check that variable, if it is true then go to NewName else return error message.


Like i said i am new about OOP. I can do what i want with functions but i want to learn class.

Link to comment
Share on other sites

The logic you have described has nothing to do with whether or not its done inside a class or not, besides, this really is a bad example of how classes work.


Generally, a class wouldn't automtically execute its functionality when instantiated.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.