JavaScript
x
<?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
JavaScript
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