Inheritance
Inheritance works just like you’d think it would. Lets say you have a class, we’ll call it parentClass just for the sake of it.
<?php
class parentClass {
}
?>
We can “extend” this class, to give it a child. So we can make a new class, and say that it is an extension of this parentClass.
<?php
class parentClass {
}
class childClass extends parentClass {
}
?>
childClass can now be said to be, well, the child of parentClass. the child class inherits all the functions and variables of the parent class. This is great if you have a generic class (that just means it can be used in a lot of situations) that has a lot of basic functions and variables, which can then be extended upon in a child class.
If you want to call a function of variable from the parent class in the child class you can just do it as you would normally with the $this variable:
<?php
class parentClass {
function x() {
print"Hello Agent X";
}
}
class childClass extends parentClass {
function y() {
$this->x();
}
}
$new = new childClass;
$new->y();
?>
This will just output “Hello Agent X”. You can do it like this if you want, or you can use the parent keyword and scope resolution operator (or just double colons :: for us common folk). This is especially useful if you have a function in the child class with the same name as a function in the parent class, and you want there to be a difference.
<?php
class parentClass {
function x() {
print"Hello Agent X";
}
}
class childClass extends parentClass {
function x() {
print"Hello Agent Y";
}
function y() {
parent::x();
}
}
$new = new childClass;
$new->y();
?>
That will just output “Hello Agent X” again. As (I think) I said in part 1, private functions and variables will only work in the original class they’re from, so you can’t call a private variable in a child class (from a parent class).
By calling a function with the same name in a child class, you override the parents definition of that function
The double colon (scope resolution operator) is an operator you’ve probably never seen or used before if you’re new to object orientated programming. It’s usually only used in tandem with the static keyword, a constant, but can be used with non-static functions and variables if used with the parent keyword. Speaking of the static keyword, I think that’s where we’re headed next.
Static, Abstract and Constant
Static and Abstract are just keywords, like public, function, etc. Static allows you to use the scope resolution operator to call functions and variables. Constants also allow you to use the scope resolution operator.
Static
As mentioned before, static is just another keyword that helps to define something in PHP.
<?php
class parentClass {
public static function x() {
print"Hello!";
}
}
parentClass::x();
?>
The above will output “Hello!”. This means you don’t have to initiate the class whenever you want to call a function or variable. Static functions and variables can not be called by initiating a class.
Abstract
Abstract is another keyword. An abstract class cannot be called directly; instead you have to create a child class which extends the abstract class, and then call that. Functions in an abstract class have to be called in the child class as well, to be able to call them. That all sounds very vague so here’s an example:
<?php
abstract class parentClass {
abstract public function x();
public function y() {
print"Goodbye";
}
}
class childClass extends parentClass {
public function x() {
print"Hello!";
}
}
$new = new childClass;
$new->x(); /* Will output "Hello" */
$new->y(); /* Will output "Goodbye" */
?>
Constants
Constants are a kind of variable that can be called using the scope resolution operator.
<?php
class parentClass {
const constant = 'Hello';
function x() {
echo self::constant;
}
}
echo parentClass::constant; /* Shows "Hello" */
$new = new parentClass;
$new->x(); /* Shows Hello (Again) */
?>
Constructors and Destructors
Constructors an and Destructors are what are known as magical methods (something I’ll go into in more detail in part 3). You can recognize magical methods by the “__” in front of them. Basically, a constructor runs automatically when the class has been initiated, and a destructor runs when the class ends. You don’t always necessarily need a destructor, because your class is basically deleted after it has run.
A constructor and a destructor look a bit like this:
<?php
class parentClass {
function __construct() {
print"I'm a construction<br />";
}
function __detruct() {
print"Bah, I've destroyed your construction";
}
}
$new = new parentClass;
?>
These will both run automatically, which is quite useful in some cases.
Quick Summary Reference Table of Key Terms
| Name/Keyword |
Function in OOP |
| Scope Resolution Operator: |
A double colon (::) used in the calling of static functions/variables, constants and when using the keyword parent or self. |
| extends: |
Used to extend a class. Extended classes are able to use the same variables/functions/constants/etc as the parent class, as long as they aren’t private |
| parent and self: |
Used with the cope resolution operator to use a funtion in the parent class or in the same class. E.g. parent::x(); or self:$variable; |
| abstract: |
When added in front of a class, function or variable, that class function or variable can only be called or used from a child class. If you’re using it on functions and variables the class must also be abstract. |
| static: |
If something is static you can call it using the scope resolution operator. This doesn’t work for classes. |
| constant: |
Like a variable, only, again, it can be called using the scope resolution operator. |
Comments
Thanks mate for the great OOP tutorial!
unfortunately there a problem with rendering php open tag (<?php) and quote ("") in your syntax highlighter.
Perhaps you can fix it soon!
Thansk!
没想到老外也搞这个,真搞。