Objects in PHP
Putting It All Together, Part I

Well done! You've reconstructed the Person class.

Let's create another class: a Dog class. It should have a public property $numLegs, which stores the value 4. Furthermore, there should be the possibility to give every dog a $name using a __constructor.

Of course we also need some methods, as a dog is able to bark(). Our dogs are also able to introduce themselves, to greet(), so we'll also need a method for this.

At last we'll need two cute dogs. :-)

That's it. Let's start!

Note: You'll find the basic syntax for the tasks in the hints, so don't worry. :-)
And although this looks like a huge task, it's actually quite easy. :-)

So this time let's create our class and add the properties.

  1. Add the <?php ?> tag.
  2. Create a Dog class.
  3. Add a public property called $numLegs, which stores the value 4 to the Dog class.
  4. Add another public property called $name, which doesn't contain a value.
  5. Add the __construct()or method, which accepts $name as a parameter.
  6. In this __construct()or method, add some code, so $this->name contains $name.
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

Syntax remainders ahead:

Adding PHP:



Creating a class:

  class Classname {
    // Code

Adding public properties (to be added in a class):

public $prop1 = 123;
public $prop2;

Adding a constructor (to be added after the properties in a class):

public function __construct($prop2) {
   $this->prop2 = $prop2;
View Preview