Skip to content
Advertisement

how can i remove ‘}’ expected error in php classes in vscode. I used these codes

<?php
class Person{
    $age;
    $firstname;
    $lastname;
    function print_name(){
    echo $this->firstname.' '.$this->lastname.' is '.$this->age.' years old.';
    }
}

$person1 = new Person();
$person1->age = 17;
$person1->firstname = "Muller";
$person1->lastname = "Thimo";
$person1->print_name();
?> 

These are the codes i used and these are the errors i’m getting

Advertisement

Answer

class property should have visibility like public ,private ,protected

so it should be like

class Person{
    public  $age;
    public $firstname;
    public $lastname;
    function print_name(){
    echo $this->firstname.' '.$this->lastname.' is '.$this->age.' years old.';
    }
}

$person1 = new Person();
$person1->age = 17;
$person1->firstname = "Muller";
$person1->lastname = "Thimo";
$person1->print_name();
?>

You can read here https://www.php.net/manual/en/language.oop5.visibility.php

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement