Property Panic (2) Codeacademy PHP Example

Property Panic (2) Codeacademy PHP Example

New syntax here. The constructor will help you add items to your class. Notice that it is using the same syntax as a class/function with brackets and the properties from inside the parenthesis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>
	<head>
	  <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
	</head>
	<body>
      <p>
        <?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("boring", "12345", 12345);
 
        echo $student->$age;
        ?>
      </p>
    </body>
</html>

Add a comment: