0

I'm very new to coding in PHP and I've been trying some courses, but I can't seem to figure this out. I wanted to display a page I had to create in a course, but it seemed like the code "broke up" after the first instance of the arrow notation. This is a simplified version of the page (but with the same error).

<html>
    <head>
      <title> Hello World! </title>
    </head>
    <body>
        <p>
            <?php
                class Dog{
                    public $numLegs = 4;
                    public $name;
                    public function __constructor($name){
                           $this->name = $name;
                    }
                    public function greet(){
                        return "Hello! My name is " . $this->name . "!";
                    }
                }

                $Doug = new Dog("Doug");

                echo $Doug->greet();

            ?>
        </p>
    </body>
</html>

Here is the output in Chrome: image

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Tom
  • 1
  • 1
    looks like your webserver doesn't know it should be interpreting this as a PHP file. Are you using Apache? What is the file name? – msturdy Nov 29 '14 at 13:56
  • save this page like php. e.g. index.php not index.html – dm4web Nov 29 '14 at 13:57
  • Change `__constructor` to `__construct` Also otherwise the sample works fine for me! Think your server don't support php OR you didn't saved the file with `*.php` – Rizier123 Nov 29 '14 at 13:57
  • Are you actually running a web server, or just attempting to load a `.php` file by opening it in your browser? You will need a web server to invoke the PHP interpreter and send its output to the browser as html. – Michael Berkowski Nov 29 '14 at 14:00
  • Oh wow I forgot about all those things, .php, the webserver... thank you! Guess I still have a lot to learn :) – Tom Nov 29 '14 at 18:01

1 Answers1

0

Here is code,

you have to change constructor to construct on line no 11

and save this file with .php extension

hope you will get the answer

<html>
    <head>
      <title> Hello World! </title>
    </head>
    <body>
        <p>
            <?php
                class Dog{
                    public $numLegs = 4;
                    public $name;
                    public function __construct($name){
                           $this->name = $name;
                    }
                    public function greet(){
                        return "Hello! My name is " . $this->name . "!";
                    }
                }

                $Doug = new Dog("Doug");

                echo $Doug->greet();

            ?>
        </p>
    </body>
</html>
Raghu Goriya
  • 78
  • 1
  • 10
  • Thanks you, the program that checked the code for me somehow accepted __constructor every single time.. – Tom Nov 29 '14 at 18:02