Ajax-loader-large-white
Objects in PHP
Property Panic (2)

Good job, now we have some properties.
But right now $teacher and $student are the same, which should be changed immediately, correct? :-)

The solution: we have to create a constructor to create different objects. This constructor is also a method, but you don't need to worry about this fact just yet.

The syntax:

public function __construct($prop1, $prop2) {
  $this->prop1 = $prop1;
  $this->prop2 = $prop2;
}

So you should remember the public keyword and the arrow notation.

Some new things:

  1. You're creating a function bound to a class (a method).
  2. The constructor method has to be called __construct().
  3. Finally, the weird way to assign the values: $this->prop1 = $prop1 means that the value you pass in the __construct() function via the new keyword is assigned to $this, which represents the object you are dealing with, and ->prop1 is the actual property of the object.

By creating a new instance using the new keyword, you actually call this __construct() method, which constructs the object. And that's why we have to pass in some arguments when we create an instance of a class, since this is how the properties get set!

Instructions
  1. Add a constructor to Person with three parameters, $firstname, $lastname and $age.
  2. In your constructor, use the three parameters to set the public properties $firstname, $lastname and $age.
  3. Change your $teacher instantiation to store new Person("boring", "12345", 12345).
  4. Add your $firstname, $lastname and $age to $student in the same manner.
  5. echo the $age of $student.
?
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

Now your code should look similar to this one:

class Classname {
  public $prop1 = true;
  public $prop2;
  public $prop3;
  public $prop4;
  public  function __construct($prop2, $prop3, $prop4) {
    $this->prop2 = $prop2;
    $this->prop3 = $prop3;
    $this->prop4 = $prop4;
  }
}
  $obj1 = new Classname("tired", "54321", 54321);
  $obj2 = new Classname("your", "name", 99);
  echo $obj1->prop1;
  echo $obj2->prop4;
index.php
style.css
project
View Preview