Object Oriented programming
...in PHP and JavaScript
...with your host hackNightly
...sponsored by Mocavo
...created using slid.es
#letsgo
Let's talk objects
Objects are the 'things' in our programs.
For instance, the Mocavo app has Users, Documents, Records, and several other objects that make it work.
All of these Objects are represented by some code in the codebase, and we use OOP to create them.
Simple User
(PHP - Class constructor)
simple user
(JS - Function constructor)
Principles of oop
-
Creating objects - (well, yeah)
- Inheritance - Creating parent (super) classes that child classes can inherit properties and methods from.
- Encapsulation - Restricting access to an object's properties or methods.
- Polymorphism - Allowing an object to take on several forms while your program is running.
Animals can teach you all you need to know about OOP : )
OOP principle #1 - Creating objects
W00t! Let's make some animals! (php)
(JS)
But wait....
-
Not all animals 'walk'.
-
Not all animals have 4 legs.
(gasp!)
Not to worry, there is a solution
We'll create new animal classes that will INHERIT some properties from the generic 'Animal' class.
(whew!)
OOP PRINCIPLE #2 - INHERITANCE
A frog! (in php)
Not so simple in js
So, what good is inheritance?
- Allows us to have more generic 'template' code like our Animal class that other objects can inherit from.
- Writing generic code helps to stay DRY. We didn't have to tell EACH animal object how to move, just the 'template' class.
- Keeps code clean by allowing clear separation of concerns. A frog can do it's thing without bothering the entire Animal kingdom.
- Saves us from trying to encapsulate all of the animal logic in the 'Animal' class.
---------It's very good---------
oop principle #3 - encapsulation
(don't touch my privates)
js (trickery)
and now for a blow your mind over the top outrageously difficult to understand universally confusing unbelievably unreal unfathomable concept.
Polymorphism
- "one name, many forms"
Just kidding
It's so easy, I taught it to you without you even knowing.
#LULZ! #evil_laugh #fooledyou
js
Sorry son, no polymorphism here.
Get better at prototypical inheritance.
[link]http://stackoverflow.com/a/9851525/541098
~ Fin~
Object Oriented programming
By hacknightly
Object Oriented programming
- 44