Forums

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

Home Forums Back End Not getting it? Reply To: Not getting it?

#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.