Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Object Inheritance

I don't really understand the Parent::__construct($name, $price, $desc) Part

Could someone please explain this better to me, Much Thanks!

5 Answers

Rodger Voelkel
Rodger Voelkel
21,736 Points

I can try to explain it.

When you use the parent key word you essentially calling that method from the parent so that you can execute it without having to do a whole new construct. So when you call Parent::__construct($name, $price, $desc) in your child class your essentially going back and accessing the __construct method of your parent class and performing the same functions that parent class does, except that when you do it within the child class your able to expand upon the construct and add additional functionality that the parent __construct did not possess.

Does that help?

Rodger Voelkel
Rodger Voelkel
21,736 Points

Creating the object uses the parameters of the parent class only. So if your parent class only allows for age and weight then those are the only variables you can pass into the object. Extending the parent class allows you to not only utilize the variables withing the parent class but also add to them there by creating an object with the age and weight variables as well as others you are extending into the class like birthplace.

Basically creating the object gives you an instance of the object that is identical to the parent, while extending the object uses the parent as a base that you can expand upon. Both have their uses.

Rodger,

fantastic, thank you! That's very helpful. So, if I understand right--both result in an inheritance of the parent's properties, but "extends" add the ability to add more properties and methods. Simpler than I thought.

I've got a strange question. The basics of PHP OOP make sense to me, but I'm still trying to understand this: what is the essential difference between creating a new object:

$bob = new Employee();

and extending a parent class?

class bob extends Employee{}

In other words, don't both result in a class/object that inherits everything from the parent class?

Thanks, -james

Hampton Paulk

They way I understand it, which is admittedly not very well, the first example is creating a new Employee named bob. Bob is the object, and the type (or "class") of object is an Employee. Without Bob, there aren't any actual Employees, just the concept of an employee.

The second example would be creating a new type of object–a "bob" type of object. But there are still no actual Employees (objects), just a second type of Employee. You could then make Employee objects of the "bob" type, which I have decided to call "bobjects" because reasons. A bobject would have the same properties and methods as an Employee, but bobjects can then have properties and methods of their own.

Not to say this process is useless. Bobjects could be a perfectly valid type of object. Perhaps the "bob" object is referring to every employee named "Bob".

$slydell = new bob();
$porter = new bob();
Oly Su
Oly Su
6,119 Points

In the first code you are creating a new instance of an object.

For the latter you are creating a new object template, which inherits some traits from the parent class.

Take the following analogy as an example:

$bob = new Employee();

Let's say the all the Employee() in this company have 2 properties - they all wear red uniform, and get paid $40 an hour, and they all have 1 duty, a method called buildRockets()

$bob, an employee no different than any other employees, wears red uniform, gets $40 per hour, and builds rockets.

Also, after invoking this function, we have an employee called $bob in the company.

class Scientist extends Employee()

In this example we've created a new employee template, the scientists. We can modify the Scientist class so that they wear white uniform.

In this case, if you created a new Scientist employee, the scientist will also get paid $40 per hour, also build rockets, but they wear white uniforms.

Furthermore, at the time you declared this class, no actual scientist has been created - you've only just created a new employee template called scientist.

Hope silly analogy helps :)

Yes!! Thank You!

Rodger Voelkel
Rodger Voelkel
21,736 Points

Christopher,

For some reason i got the email of your question yet the forum doesn't show updated to me, regardless what I noticed in the code you linked is that your naming your __construct as __conctruct when your creating your functions. That is likely what is causing your issues in both the parent and child class.