Object Oriented PHP for Beginners: Steps 18 – 22
Step 18:
Inheritance – reusing code the OOP way
Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes.
Why do it?
Doing this allows you to efficiently reuse the code found in your base class.
Say, you wanted to create a new ’employee’ class … since we can say that ’employee’ is a type/kind of ‘person’, they will share common properties and methods.
… Making some sense?
In this type of situation, inheritance can make your code lighter … because you are reusing the same code in two different classes. But unlike ‘old-school’ PHP:
- You only have to type the code out once.
- The actual code being reused, can be reused in many (unlimited) classes
but it is only typed out in one place … conceptually, this is sort-of
like PHP includes().
Take a look at the sample PHP code:
// 'extends' is the keyword that enables inheritance class employee extends person { function __construct($employee_name) { $this->set_name($employee_name); } } |
Step 19:
Reusing code with inheritance: part 2
Because the class ’employee’ is based on the class ‘person’, ’employee’ automatically has all the public and protected properties and methods of ‘person’.
Nerd note: Nerds would say that ’employee’ is a type of person.
The code:
class employee extends person { function __construct($employee_name){ $this->set_name($employee_name); } } |
Notice how we are able to use set_name() in ’employee’, even though we did not declare that method in the ’employee’ class. That’s because we already created set_name() in the class ‘person’.
Nerd Note: the ‘person’ class is called (by nerds,) the ‘base’ class or the ‘parent’ class because it’s the class that the ’employee’ is based on. This class hierarchy can become important down the road when your projects become more complex.
Step 20:
Reusing code with inheritance: part 3
As you can see in the code snippet below, we can call get_name on our ’employee’ object, courtesy of ‘person’.
The code:
<?php include("class_lib.php"); ?> <?php // Using our PHP objects in our PHP pages. $stefan = new person("Stefan Mischook"); echo "Stefan's full name: " . $stefan->get_name(); $james = new employee("Johnny Fingers"); echo "--> " . $james->get_name(); ?> |
This is a classic example of how OOP can reduce the number of lines of code (don’t have to write the same methods twice) while still keeping your code modular and much easier to maintain.
Step 21:
Overriding methods
Sometimes (when using inheritance,) you may need to change how a method works from the base class.
For example, let’s say set_name() method in the ’employee’ class, had to do something different than what it does in the ‘person’ class.
You ‘override’ the ‘person’ classes version of set_name(), by declaring the same method in ’employee’.
The code snippet:
<?php class person { protected function set_name($new_name) { if ($new_name != "Jimmy Two Guns") { $this->name = strtoupper($new_name); } } } class employee extends person { protected function set_name($new_name) { if ($new_name == "Stefan Sucks") { $this->name = $new_name; } } } ?> |
Notice how set_name() is different in the ’employee’ class from the version found in the parent class: ‘person’.
Step 22:
Overriding methods: part 2
Sometimes you may need to access your base class’s version of a method you overrode in the derived (sometimes called ‘child’) class.
In our example, we overrode the set_name() method in the ’employee’ class. Now I’ve used this code:
person::set_name($new_name); |
… to access the parent class’ (person) version of the set_name() method.
The code:
<?php class person { // explicitly adding class properties are optional - but // is good practice var $name; function __construct($persons_name) { $this->name = $persons_name; } public function get_name() { return $this->name; } // protected methods and properties restrict access to // those elements. protected function set_name($new_name) { if ($this->name != "Jimmy Two Guns") { $this->name = strtoupper($new_name); } } } // 'extends' is the keyword that enables inheritance class employee extends person { protected function set_name($new_name) { if ($new_name == "Stefan Sucks") { $this->name = $new_name; } else if ($new_name == "Johnny Fingers") { person::set_name($new_name); } } function __construct($employee_name) { $this->set_name($employee_name); } } ?> |
Notes: Using the symbol:
:: |
… allows you to specifically name the class where you want PHP to search for a method:
'person::set_name()' |
… tells PHP to search for set_name() in the ‘person’ class.
There is also a shortcut if you just want refer to current class’s parent – by using the ‘parent’ keyword.
The code:
protected function set_name($new_name) { if ($new_name == "Stefan Sucks") { $this->name = $new_name; } else if ($new_name == "Johnny Fingers") { parent::set_name($new_name); } } |
Conclusion
We’ve only touched on the basics of OO PHP. But you should have enough information to feel comfortable moving forward.
Remember that the best way to really have this stuff sink in, is by actually writing code.
I would suggest creating say 10 simple objects that do simple things, and then use those objects in actual PHP pages. Once you’ve done that, you will feel very comfortable with objects.
If you guys/gals are interested in more, let me know.
By: Stefan Mischook
Questions?
Just post them on the PHP forum.