Answer the question
In order to leave comments, you need to log in
What is public and why doesn't a PHP function work?
Good evening!
I am learning OOP in PHP at CodeCademy.
I got this code:
<!DOCTYPE html>
<html>
<head>
<title>Reconstructing the Person Class</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<!-- Your code here -->
<?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;
}
$teacher = new Person("boring", "12345", 12345);
$student = new Person("Alex", "Bagirov", 15);
echo $teacher->isAlive;
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
echo $teacher->greet();
echo $student->greet();
?>
</p>
</body>
</html>
Parse error: syntax error, unexpected T_PUBLIC on line 18
Answer the question
In order to leave comments, you need to log in
public function __construct must be defined in the class:
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question