socketwench.github.io/OOPforObjectHaters
Nothing!
(Well, almost nothing.)
Easy to write and use.
"Assembly line" code.
No simple way to extend
No easy way to encapsulate state
No struct
leads to Arrays Of Doom.
Weak typing makes testing, documentation hard.
Larger structures harder to see.
Functions (and hooks) are a golden hammer.
You don't, but it helps.
Bad IDEs can reflect poorly on the language.
Bad initial experience can taint a language.
C++ has weirder, less modern OOP.
Java is a little too object happy...
PHP4 wasn't even finished. Ugh!
Raphael, 1511
Abstract, only exists in your mind
Represents a class of real things
Physical and "concrete"
May differ from the ideal...
...but identifiable as part of the same class
class Chair;
Extends the parent, or "base" class
Child class "inherits" aspects from the parent
class ComfyChair extends Chair;
class OfficeChair extends Chair;
class WeirdChair extends Chair;
Child class is a decedent of parent class
class Robot {
// More stuff here.
}
"Class Variables"
Encapsulates state in the class
class Robot {
protected $myVariable = 'Initial Value.';
}
Precedes a property definition
public
, private
, or protected
Child classes inherit the property
Everyone can read & write the property
Child classes do not inherit the property
No one but the defining class can see or use it
Property stays "within the family".
Unrelated classes do not see or have it.
Define them like you would any other variable
class Battery {}
class Robot {
protected $battery;
}
Hosting class possesses hosted class
"Class Functions"
Gives the class knobs and buttons
class Robot {
public function myMethod($param) {
// Method body
}
}
Controls who can call, not see.
public
= Everyone
private
= Methods in the same class
protected
= Only classes in the same family
Plays a special role in the language
"Returns" an object of the class' type
May have parameters like other methods
class Robot {
public function __construct($someOption = NULL) {
// Build the object.
}
}
They're used to build your house, not be your house
Classes are defined in your code
One class = zero or more objects
"Instantiating a class"
$myRobot = new Robot();
$otherRobot = new RobotBatteriesNotIncluded($battery);
Calls the class's constructor method
"returns" a new object
->
or "arrow" operator
$myRobot->myVariable = 'Some other value.';
$myRobot->myMethod($param);
class Robot {
public function setMyVariable($param) {
$this->$myVariableName = $param;
}
public function getMyVariable() {
return $this->myVariableName;
}
}
(The above is called a "setter" and "getter".)
$this
keyword
Refers to object that owns the method
$normalGhost->slime($ghostBuster);
$slimer->slimeLots($ghostBuster);
switch(get_class($unknown_ghost)) {
case "Ghost":
$unknown_ghost->slime($ghostBuster);
break;
case "Slimer":
$unknown_ghost->slimeLots($ghostBuster);
break;
}
class Ghost {
public function slime($ghostBuster) {
// Normal slime.
}
}
class Slimer extends Ghost {
public function slime($ghostBuster) {
// Lots more slime!
}
}
Refers to the current class' parent class.
Not a variable! Must be accessed statically.
Like ->
, but static
Accesses the class, not the object
class Slimer extends Ghost {
public function __construct() {
$color = 'green';
parent::__construct($color);
}
}
Works on any method!
All classes in this family should do this...
...but each does it completely differently.
class Ghost {
abstract public function frighten($person);
}
Abstract method = abstract class.
abstract class Ghost {
abstract public function frighten($person);
}
Fancy word for "same name, different action".
General software engineering modeling language
Not OOP specific, but really useful
So...don't diagram everything
Only the parts where a picture would help
Visualizes what a class is made of
How classes relate structurally to each other
Visualize when objects are created/destroyed
When and what classes "say" to each other
http://www.uml.org/
You to do something
I agree to only ask you to do things you can do
Like a class, but lighter
Can't be created by itself, but implemented by classes
interface AstroMech {
public function beepBoop();
protected function navigateXwing($coordinates, $speed);
}
They only define what they can do...
...not how they do it or what they did.
No method body
Define params as normal
implements
keyword
class R2DTwo extends BaseDroid implements AstroMech {
...
}
You can only inherit once...
...but implement multiple interfaces!
class ThreeCPO implements ProtocolDroid, LoadLiftProgrammer {
...
}
What an object can do is more important
Define a completely different class "lineage"
Excellent place for documentation
Now you only depend on the interface, not class
function myFunction(AstroMech $droid) {
// Do something.
}
extends
keyword, just like classes
interface R2Droid extends AstroMech {
public function hide($lightsaber);
}
Puts classes into a named box
Names need be only unique in each box
$myDate = new \MacLeod\Date();
$yourDate = new \Kurgan\Date();
Syntax differs greatly across languages!
<?php
namespace \Movies\Highlander;
Must be the first line!
use \Really\Long\Namespaced\Class\Name;
$myName = new Name();
Imports class and gives it a new name
use \Movies\Highlander\Kurgan\Date as BadGuy;
$myVar = new BadGuy();
Each language does it differently
Some common patterns have emerged
File name same as your class
Use your languages normal extension
No *.inc for PHP
Match your namespaces
Nested namespace = nested directory
$myDate = new \Movies\Highlander\MacLeod\Date();
Conventions differ between languages and communities
Modern PHP uses <project_dir>/src
Not in steps
A little UML goes a long way
Find one you don't hate immediately
Helps find what you need faster
You don't need to keep it all in your head
Design Patterns by Erich Gamma et. al
socketwench.github.io/OOPforObjectHaters
Background by Artwyrd