OO = Object Oriented
What does this mean? In short: an object is "something." In the real world it`s touchable, like a car. A car is comprised of different parts, like wheels, an engine, a steering wheel et cetera. All these different carparts are objects as well.
What OO does is actually quite similiar, you create a Car object. On that Car object, you add other Objects like an engine or wheels. The whole idea behind this is so you "filter" out information. The other thing about these parts is that they belong to something, right? An engine belongs to a car So you could say that the engine is an extension of the car.
An engine itself can have parts too, vauls, pressure pumps, all these parts of an engine belong to an engine. All cool right?
OK time to get to OO land. What does this mean? Well, first of, when we create a Car object, we don`t want to know what engine it`s using, only that the Car object is actually using one. if we did want to know what engine we have, we could ask the Car object for it, and it would respond in saying what engine it was. However, the engine doesn`t give all information as to what pumps and vauls are used, so we can ask the Engine object for it, please tell us what objects you have inside.
This whole extending things ( Engine extends Car or Vaul extends Engine ) is called: inheritance. Why call it like this? because a car example sucks.
Let`s do another one, let`s say we have an Animal Object.
We have 2 sorts of animals right? vertibrate and invertibrate
Now, what do ALL animals have? A biological name for example
Both vertibrate and invertibrate have those properties ( < important )
What does a vertibrate animal have?
A vertibrate! So the properties for a vertibrate object is that is has a vertibrate.
Now, what kinds of vertibrate are there?
There are fish, amphibians, reptiles, birds, and mammals.
Ok, let`s focus on mammals for now.
So we now have a mammal object which extends a vertibrate ( and animal )
What are key properties for mammals?
Well, for one, the give birth to live young, there`s a property we can share for all mammals.
Ok, next up: what kinds of mammals do you have?
Enough, like 5000 or something, which is too much. There are some more categories, but i`m going to pick cat like mammals
So CatLikeMammals extends Mammals ( which extends Vertibrate which extends Animal )
What`s specific on catLikeMammals? They have hair, have 4 legs, are carnivor, grow a tail, et cetera. All properties CatLikeMammals have.
In code this would look like:
Code: Select all
<?php
class Animal
{
public $biologicalName = 'Animal';
}
class Vertibrate extends Animal
{
public $vertibrate = true;
}
class Mammal extends Vertibrate
{
public $givesBirthToLiveYoung = true;
}
class CatLike extends Mammal
{
public $hasFourLegs = true;
public $hasHair = true;
public $hasTail = true;
public $isCarnivore = true;
}
class Lion extends CatLike
{
public $height = '50inch';
public $length = '200inch';
public $width = '30inch';
public $livesIn = 'Africa';
}
?>
Ok if i wanted to create a Lion, in PHP ( or any other language ) I use the new keyword:
Code: Select all
<?php
$lion = new Lion;
?>
So I can now get to so if it is a vertibrate or not:
Code: Select all
if($lion->isVertibrate){
echo "Yay, i`m a vertibrate";
}
A lion has UNIQUE characteristics like it`s height, width, average weight and location. These are properties ONLY for lions ( in our example ) So only a Lion Object can access them.
Now, there`s a problem: we have a biological name set in our Animal object. A lion`s biological name is not "Animal". It should be "Panthera leo."
So how we change that?
Well, we add a constructor to our Lion object. A constructor is the FIRST function ( = method in OO terms ) that will be executed once we create a new Lion object.
How do we do this?
Code: Select all
class Lion extends CatLike
{
public function __construct()
{
}
}
Oh, while we are on the topic, in PHP you can also use the name of the class as the constructor. So public function Lion(){} works pretty much the same. But __construct() is recommended.
Ok, we have a constructor, what`s so special about that? Well, like I said, it will be executed ALLWAYS. Regardless of anything.
Now, we know the original name for a lion, let`s change that!
Code: Select all
public function construct(){
$this->biologicalName = "Panthera leo";
}
Also, if you looked at the code and was all like "uh Xaleph, what`s this $this thing?" Glad you noticed! The THIS keyword is a reserved name for objects. $this allways refers to it`s object. So in our example $this refers to the Lion object.
However, look at the following code:
Code: Select all
<?php
class Lion extends CatLike
{
function setCuteNickname($name)
{
$this->nickname = $name;
}
function getCuteNickname()
{
return $this->nickname;
}
}
$lionOne = new Lion();
$lionTwo = new Lion();
$lionOne->setCuteNickname("Snuggles");
$lionTwo->setCuteNickname("Gorgeous");
echo $lionOne->getCuteNickname(); // what will it produce?
echo $lionTwo->getCureNicknime(); // and this one?
There are a plethora or rules out there, but that`s all for the next time. I hope you like the tutorial so far, if anyone wants to see a part 2, i`m glad to write it!