What are the differences in handling classes between PHP and JavaScript?
In PHP, classes are defined using the `class` keyword followed by the class name. Properties are declared using `public`, `private`, or `protected` keywords. In JavaScript, classes are defined using the `class` keyword as well, but properties are declared inside the constructor function using the `this` keyword. Additionally, PHP supports inheritance through the `extends` keyword, while JavaScript uses the `extends` keyword as well but with a slightly different syntax.
<?php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function greet() {
return "Hello, my name is {$this->name} and I am {$this->age} years old.";
}
}
$person = new Person('John', 30);
echo $person->greet();
?>