One of the key techniques of the object oriented programming paradigm is the ability to have your classes inherit functionality from other classes. This is one of the most powerful advantages of object oriented programming. It will allow for a whole new dimension of reusable code in your programs, as well as an increased universality in the method calls from your classes. Inheritance is critical to large, and flexible projects like a CMS or Lexy sites. Object oriented PHP (OOPHP) has great functionality for inheritance, but a good deal of it can be a bit intimidating to developers who aren't sure of what it can do and when to do it.
In PHP, to have a new class get all of the methods and variables from another class, you would do something like this:
require_once('TemplateClass.php');
class myClass extends TemplateClass {
}
Remember, any variable or function declared private can not be used in a class extending it. You have to either use protected or public before the declaration of a method or variable. Example:
public $myArray = array();
protected function printMyArray() {
}
This is usually referred to as “scope”, but sometimes in PHP it is referred to as “visibility”. Sometimes, however, you want a slight change to a variable or a function. That's when “overloading” comes in.
Overloading is simple. You just declare a function again. So, if you extend a class with a function called “getValues”, you can declare it again in the extending class to do something different, and choose whether to call the parent function or the overloaded one. Example:
class parentClass {
protected function getValues() {
}
}
class childClass extends parentClass {
protected function getValues() {
// Put something different in here than what was in the parent class.
// Then you can even do something cool like call the original function in the overloaded one.
parent::getValues();
}
}
If it is crucial that the functionality of a, well, function doesn't change in the children classes that may inherit it, then you can declare the variable or method “final”. Such as this:
final protected function getValues() {
}
This may be a good idea for methods that get important variables or other data from the parent class, or if the functionality of one method depends upon another method working a certain way. You don't want someone to accidentally break an entire class. One of the most important things to remember about overloading is the the reference of variables and functions using “late static bindings”.
This may sound like one of those advanced features you don't really care about, but it is anything but. Forget the confusing name for a bit if you have to, but pay attention to the details because this could save you hours of bashing your head against the keyboard, burning a pile of laptops, and marching through the streets shouting “flesh power” as I know I'm often tempted to do when I don't understand why the computers are making my life so miserable. Just remember this, if getValues calls getName, and you overload getName, then getValues will not be calling the overloaded function. That is, not unless you overload getValues, or if you call the getName function in the parent class' getValues like so:
class parentClass {
public function getName() {
}
public function getValues() {
static::getName();
}
}
Now when a class extends “parentClass” and the function getName is overloaded, any calls of getValues will use the child's overloaded version of getName. I'm not trying to confuse any new developers with complicated inner workings of PHP, but hopefully if you ever have a freak out about overloaded functions not doing what you want them to, you'll now know one of the usual suspects that have caused so many others to develop more ulcers than PHP functions.
One of the most important things to keep in mind for learning developers, or really any developers, is that lessons hit home best when you actually need to do something. The chances are nearly assured that if you develop PHP long enough, you'll want to extend from one of your old classes so that a couple of unique classes can use some of the same stuff. The important thing to keep in mind is the fact that you can do that, even if you need to look up the manual again, and what sort of snags you may get into with declaring the scope or visibility of your variables and functions. Rote memorization is not what is asked for in the field of development, but you need to have a vague memory of what sort of things can be done so that when you're faced with a problem, you know where to go to get the solution.
VivaNet2.0 constructed website, myUSAi.org according to our exact technical specifications and with our required timeframe and budget. We liked VivaNet2.0...