www.webdeveloper.com
Results 1 to 7 of 7

Thread: help with return function

  1. #1

    help with return function

    hi all,
    i have one question around return function
    for ex.
    PHP Code:
    <?php
        
    class Employee
        
    {
            private 
    $name;
            
    // Getter
            
    public function getName() {
                return 
    $this->name;
            }
            
    // Setter
            
    public function setName($name){
                
    $this->name $name;
            }
        }
        
    $azer=new Employee();
        
    $azer->setName('sahib');
        echo 
    $azer->getName();
    ?>
    why here we is used two functions. one serve as getting values and the other one serve as setting values. Why we cannot use for this both getting values and setting together inside one function. such as below.

    PHP Code:
    <?php
        
    class Employee
        
    {
            private 
    $name;
            
    // Getter
          
            // Setter
            
    public function setName($name){
                
    $this->name $name;
    return 
    $this->name;
            }
        }
        
    $azer=new Employee();
        
    $azer->setName('sahib');
        echo 
    $azer->setName();
    ?>
    can anyone help me if possible
    thanks in advance

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    you could do it that way but it is not the way it is normally done.

    A class (object) method (whether it is PHP, Java etc doesn't matter ) is meant to perform a single function only.

    For example, in your code you might need to "set" the value of a class property at some stage without needing it to then be returned at that point and elsewhere in your code you might just need to "get" the value of the property without having to set it to its current value first, like you do in your suggestion.

  3. #3
    thank you very much
    but i again dont understand why we cannot set values and get values inside one function
    for ex. we build first the fucntion that sets values and second fucntion is for retunrning values that is set at the first function. Cannot we set and return values inside one function.
    thanks for attention

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    no problem

    I did say you can do both set and get in one function if you like. It's just not normally done that way for the reasons I expressed earlier.


    With your code:

    PHP Code:
    $azer=new Employee(); 
        
    $azer->setName('sahib'); 
        echo 
    $azer->setName(); 
    imo you have an unnecessary line of code because instead of the 3 lines you have above you could get the same output with:

    PHP Code:
    $azer=new Employee(); 
        echo 
    'sahib'

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    The code you posted is not valid. You cannot define an argument and then not provide in calls from client code. You could make the argument optional and have logic to handle getting or setting conditionally, but why would you want to do that? Surely it is much simpler and clearer to have get and set functions?

  6. #6
    thank you very much
    can you show me (if possible) the need of creating getter function

  7. #7

    resolved

    solved

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles