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 trialvaleriuv
20,999 PointsJust finished Object Oriented PHP Basics. My thoughts
Just wanted to share my thoughts and suggestions.
Although the course in itself is not hard, I still don't understand the logic of PHP behind class inheritance (especially the constructor), interfaces and exceptions. I googled a bit and read a few external tutorials.
I wish there were more simple real world examples of use of classes and objects in the course, or may be this is the material for another course.
7 Answers
Jon Schiedermayer
7,709 PointsI just took that course as well, and I think interfaces and exceptions will be topics that future courses will get into in greater depth.
Classes and Objects - here's my thought with Indranil's example in mind...
A class: We know that there is such a thing as a book. It has properties (title, author, weight, number of pages) and methods (ShowWordsOnPage this is more abstract).
A class object: This is an an instance of a book that you are holding. It's called Moby Dick, by Melville, weighs a ton, has a lot of pages, and when you turn to page 444 there are words on the page for you to read.
Or, another example...
classPerson:
properties: name, height, age
methods: sleep(), eat(), excercise()
objectPerson:
properties: Dan, 6'2", 32 object's properties are set with values
methods: sleep(), eat(), excercise() make the object do things
Indranil Tiwary
9,341 PointsYou know I faced the same problem.. I wish to help you..
Class and objects in real world type example: Class can be thought of as a book shelf in a library. The books are different methods, maybe a simple method (or function) to add two numbers.. Or any other method made by us.. Object can be thought as a variable that calls that book shelf whenever required. A object in more simple terms is the variable that calls a class. Syntax: class_name variableObjectName = new class_name(); It commits an object. And we can make several objects of a class each different from each other..
We change one object .. The other is not affected..
Class Inheritance: Son takes all the properties of father .. Its inheritance..
Exceptions: You are doing an error here.. 1/0 = indefinite.. You want the program to check whether here is the exception so you do exception handling.. Catch and throw stuff.
valeriuv
20,999 PointsYes, I guess a better analogy is the class as a blueprint for the books and each object is a different book, with different text inside.
However, what does it mean that the child class constructor passes the values through the parent constructor? The notations are a bit confusing. Also, I am not sure why the notation for class values (like name, description) were exactly the same as the constructor parameters? That was not the case in the challenge, which gave me a bit of a headache.
I'm looking forward to more PHP courses hopefully with examples.
valeriuv
20,999 Points@Jon : the Person class is a great analogy.
Robert Walker
17,146 PointsThe constructor has always confused me, though I get what it is I find it hard to know when to use it and when not to.
As of right now I think of the constructor as creating an object such as a person but with already defined methods like height 6' arms 2 and legs 2.
Even though I understand that I don't understand why I would want that if that makes sense at all lol.
I am still looking for that lightbulb moment where the constructor suddenly makes total sense and I understand the value of it totally.
Sadly the course didn't really do that for me, I'm sure once that lightbulb goes off I will see it in the course but right now its one of those things I stick under: I sort of get it.
I really wish they would build a framework start to finish, not copy something that has already been made or even go over something already made, I want to see something start to finish from 0 folders and 0 logic set out.
I would learn so much faster and have such a better grasp of things from that, following the logic from the ground up would be amazing, taking you through things like sessions, cookies, building a custom CMS / admin panel, handling errors and writing them / emailing them depending on the error.
While I know how to do these things on their own watching something that incorporated all of those things into one and used correctly would really help me on my journey. I get a little frustrated that things seem to move so slow in the courses and other times I get frustrated because things jump ahead way too fast missing out what I would think are logical steps in the ladder.
Without ranting on much longer, the course was good but was let down massively by using frameworks, template engines and not building something from scratch.
I think building a small site with a login, register and profile page would of been a far better way to learn this. Building out your own pages table in a database and actually building things like auto loader would of been a far better look into the OOPhp world than using pre build systems, a good half of the course is spent learning parts of the pre builts used for the example project.
EDIT: Just wanted to point out I was talking all the OOP courses in the track not just this particular course within it.
Jon Schiedermayer
7,709 PointsThe further you go in OOP the more this will make sense, because you'll need to use class objects a lot, but it could be the fault of my example.
The Person Class has properties and methods.
The object which is a new instance of the Person Class has all of the properties and methods of the Person class, and the constructor is how the VALUES are assigned within the new object.
So in my example above, you have a class called Person with properties like: public string name, public string height, public int age.
Lets call the object danPerson, which is a new Person. We use the constructor to set the values of danPerson:
(property = value) name = "Dan"; height = "6'2"; age = 32;
So the danPerson class object has the same properties (name, height, age) but with it's own unique values.
Robert Walker
17,146 PointsMuch better way of explaining it out Jon but what I don't understand is why that comes in handy at all.
I can create a class without a constructor and sill have multiple instances of Person, so I struggle to see the need for a constructor or when I should use it.
I could have a Person class and it has properties of:
Name Age Height Address Number of cars owned Pets Access level
Lets say I want to assign these properties to a session so this Person can move around my site and have access to the things only this Person should.
I would just build a function:
getAccessLevel($id) { sql query: select access_level from person where id = $id
assign session['access'] level 5
}
I am not sure why I would ever want to define properties when creating a new instance when I am likely always getting my information from a database.
I guess you could say my problem is I don't understand the value of this over just creating classes without it.
Jon Schiedermayer
7,709 PointsI'm no expert on this, just a student like you, so I'm not totally sure about your use of SQL with PHP there, so I can't say much about it. The class constructor is a standard aspect of OOP - in C# we often make several constructors per class (1 default, the rest custom) which allow for instantiation of class objects that have different sets of properties and functions, depending on what you need.
Sorry if that doesn't help. This might be beyond my ken.