OOP - Following this tutorial

Status
Not open for further replies.

Swaggots

Member
Sep 22, 2013
98
18
So i'm following this tutorial and this is the code i'm up to.

PHP:
<!DOCTYPE html>
      <?php
       
        class Dog {
            public $numLegs = 4;
            public $name;
           
            public function __construct($name) {
                $this->name = $name
            }
           
            public function bark() {
                return "Woof!";
            }
           
            public function greet() {
                return "Hello, my name is" . $this->name ".";
            }
           
        }
       
            $dog1 = new Dog("Barker")
            $dog2 = new Dog("Amigo")
           
            echo $dog1->bark();
            echo $dog2->greet();
       
       
      ?>

It's giving me the following error.

Parse error: syntax error, unexpected '}' in CODE on line 9
Errors parsing CODE


Can anyone explain why please.
 
Figured it out myself.
I was missing a . between $this->name and "."
 

brsy

nah mang
May 12, 2011
1,530
272
That isn't why that error came up. The error came up because you forgot to add a semi-colon after "$name".

The code should be:
PHP:
<!DOCTYPE html>
      <?php
      
        class Dog {
            public $numLegs = 4;
            public $name;
          
            public function __construct($name) {
                $this->name = $name;
            }
 

Swaggots

Member
Sep 22, 2013
98
18
That isn't why that error came up. The error came up because you forgot to add a semi-colon after "$name".

The code should be:
PHP:
<!DOCTYPE html>
      <?php
     
        class Dog {
            public $numLegs = 4;
            public $name;
         
            public function __construct($name) {
                $this->name = $name;
            }

It doesn't matter as the } is right after it, kinda like in css.
 

Khalil

IDK
Dec 6, 2011
1,642
786
The edit brsy gave you would still return an error.

Here's the fix:

PHP:
class Dog
{
    public $numLegs = 4;
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function bark()
    {
        return 'Woof! ';
    }

    public function greet()
    {
        return 'Hello, my name is '.$this->name;
    }
}

$Dog1 = new Dog('Barker');
$Dog2 = new Dog('Amigo');

echo $Dog1->bark();
echo $Dog2->greet();
 

Swaggots

Member
Sep 22, 2013
98
18
Figured it out myself.
I was missing a . between $this->name and "."

The edit brsy gave you would still return an error.
Here's the fix:

PHP:
class Dog
{
    public $numLegs = 4;
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function bark()
    {
        return 'Woof! ';
    }

    public function greet()
    {
        return 'Hello, my name is '.$this->name;
    }
}

$Dog1 = new Dog('Barker');
$Dog2 = new Dog('Amigo');

echo $Dog1->bark();
echo $Dog2->greet();

Thanks anyways.
 
Status
Not open for further replies.

Users who are viewing this thread

Top