This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course » View Exercise

715 points
Ef5e90da9a9027045847b2fe058341ac?s=140&d=retro
Submitted by
wangyegp
over 2 years ago

Why no semicolons after methods?

<?php
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
};

public function greet() {
return "Hello, my name is ".$this->firstname." ".$this->lastname.". Nice to meet you! :-)";
};
};
$teacher = new Person("boring", "12345", 12345);
$student = new Person("Florence", "Wang", 20);
echo $teacher->isAlive;
echo $student->age;
echo $teacher-> greet();
echo $student-> greet();
?>

This code doesn't work because of the semicolons right after the methods. Why shouldn't I use semicolons to end a method?

The reason is not that it's not a line of code. I could use ; to end a class.


0 votes

permalink

I was wondering about this myself. Coming from javascript to this kinda threw me for a loop. While it is a line of code, the 'string' doesn't end with that statement. If you put it all together, it'd look similar to "public function greet() { return "Hello, my name is " . $this->firstname . ". Nice to meet you! :-)";} (then you'd finish the class stuff) then finally };.

901 points
Picture
Submitted by
Zachary Burris
over 2 years ago