Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Not getting it?

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #183339
    chrisburton
    Participant

    Ok. So I just jumped in and tried an example and I can’t seem to get an output. I feel like it’s something quite miniscule but not sure what exactly. Is it my syntax or arguments?

    <?php
        class animal {
            function name ($type='') {
               return $this->name = $type;
            }
        }
    
        $animal = new animal('dog');
        var_dump( $animal->name() );
    
        //Output: string(0) ""
    ?>
    
    #183340
    Ilan Firsov
    Participant

    First thing you don’t have a constructor that takes arguments, so the line $animal = new animal('dog'); does nothing with the argument you provided

    Second thing is that when you call the name function without arguments it defaults to name=” which is then returned.
    I’d do something like this:

    
    
    function name ($type) {
        if(!empty($type)) {
            $this->name = $type;
            return $this->name;
        } else {
            return $this->name;
        }
    }
    
    
    #183341
    chrisburton
    Participant

    First thing you don’t have a constructor that takes arguments, so the line $animal = new animal(‘dog’); does nothing with the argument you provided

    Ah __construct is what is required when using new.

    <?php
        class animal {
            public function __construct($name) {
               $this->name = $name;
            }
    
            public function name() {
                return $this->name;
            }
        }
    
        $animal = new animal('dog');
        var_dump($animal->name());
    
        //Output: string(3) "dog"
    ?>
    

    I just figured I could do this with only one function.

    #183360
    Alen
    Participant

    I just figured I could do this with only one function.

    Well there is only one method (function). The constructor is responsible for constructing the object. It initializes the values you pass. The getter method is just that, a way to get the value.

    
    class Animal {
      
      # Property
      public $name;
      
      # Constructor
      function __construct($name)
      {
        $this->name = $name;
      }
      
      # Getter
      public function getName()
      {
        return $this->name;
      }
      
    }
    
    $animal = new Animal('Dog');
    
    echo $animal->getName();
    
    #183399
    chrisburton
    Participant

    Thanks @AlenAbdula. I see you added the public $name property. I see that here and there within tutorials but I also see it omitted. When should I set this and when can I leave it out?

    #183401
    __
    Participant

    I see that here and there within tutorials but I also see it omitted. When should I set this and when can I leave it out?

    As a rule, you should always define the properties your class uses. This makes it easier to plan/organize your code, and lets you recall at a glance what it does.

    This also goes back to your public/private question (though if you’re still concentrating on other things feel free to ignore this part for now; read it when you’re ready):

    If you just assign a property (like $this->name = $name; in your constructor), it will be “public.” There’s not much effective difference in declaring the property beforehand, in the class definition, but it does make plain that the property exists and can be relied upon.

    “Public” means that the property can be accessed/assigned/changed from anywhere. So, this sort of thing can happen:

    $dog = new animal( 'dog' );
    $dog->name = 'raccoon';
    
    $dog->name();
    // prints "raccoon"
    

    If you want to make sure this cannot happen, you can make the property private:

    class animal {
    
            private $name;
    
            public function __construct($name) {
               $this->name = $name;
            }
    
            public function name() {
                return $this->name;
            }
        }
    

    Then:

    $dog = new animal( 'dog' );
    
    $dog->name = "raccoon";
    // fatal error: Cannot access private property animal::$name …
    
    $dog->name();
    // prints 'dog'
    
    #183408
    chrisburton
    Participant

    @traq The only part that is confusing is that you’re using getName, where is that coming from since you never specified that function?

    #183410
    __
    Participant

    Sorry, I used the method name from Alen’s example (but copied the class definition from yours). Fixed.

    edit
    I usually name “getter” and “setter” methods get{propertyName} and set{propertyName}, just to make their purpose obvious.

    Also, if you decide to use private (or protected) properties or methods, it is a very common convention to start their name with an underscore (i.e., $_name instead of $name). Again, it just keeps things easy to read and understand.

    #183411
    chrisburton
    Participant

    Thought I was losing my mind for a second. Now that makes much more sense to me. I’m going to try to find other examples I can use to practice. Thanks, guys.

    #183412
    chrisburton
    Participant

    Also, if you decide to use private (or protected) properties or methods, it is a very common convention to start their name with an underscore (i.e., $_name instead of $name). Again, it just keeps things easy to read and understand.

    Ah, yes. Saw this too in tutorials. Thanks for that.

    #183445
    chrisburton
    Participant

    Just leaving this here as it’s incredibly well written and helps explain public, private and protected methods.

    http://stackoverflow.com/a/21902271/938664

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Back End’ is closed to new topics and replies.