Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,341 members
- 696,913 forum posts
- 11 blog posts
Tutorials
OO PHP Part 1: OOP in Full Effect
Views: 38563
2 Defining how objects should behave
2.1 Classes
Classes are the (partial) definition of an object. Also referred to as a “object blueprint”, the class defines how the object will look after first instantiation, and how it will behave in response to operating on it. However it also possible to operate within the scope of a class without an instantiation. You will read more about that in the “Scope Resolution Operator” section.
Consider this extremely simple example of a class:
$this is a reserved variable name, referring to the current instantiation of the object. By instantiating, we create a new Dog. Don’t mind the public keyword for now.
The initial state of this dog is that it is pretty hungry.
Echoes:
hell yeah.
So we feed our newly instantiated dog a treat, to modify its state.
Yep, doggie isn’t as hungry as before:
Will echo:
not so much.
Doggie is happy enough, moving on.
2.2 Inheritance
Classes can have parent and child classes. When an object is instantiated by name of a child class, PHP looks for parent classes and starts combining the declarations, from the top, to create the initial object. This is called inheritance. It isn’t such a hard concept to grasp: a child inherits anything from its parents and ancestors that isn’t redefined on the way down. If a class lower in the chain has a property or method with the same name as one higher in the chain, the last declaration is used for requests to the object.
To declare one class as a child of another, use the extends keyword.
Like with most popular languages, in PHP one class can only extend a single other class. An infinite number of classes can be derived from (extending) a single class though.
A simple example:
Method eat is overridden, because doggie only likes cookies. But, because all animals are hungry when you don’t feed them, the initial state of $hungry need not be defined in Dog. The fictional Bird, Cat and Piggy could all extend Animal.
Class Animal is unaware of the fact that it is being extended; there are no references to Dog whatsoever. Say Animal extended another class called LifeForm, and I instantiated Animal, only methods and properties of Animal and LifeForm would be included in the object.
2.3 Constructors
Constructors are a way to define the default behaviour (set the default state) upon instantiation. You can, as shown in the previous example, define the default state by setting default values for properties. A constructor is nothing more than a method that is executed when instantiating an object. This is referred to as initializing the object.
An object isn’t fully initialized, constructed, until the constructor method has completed (if present). A constructor method isn’t required, our previous examples worked just fine without them. Another thing that should be noted is that only the constructor of the class used to instantiate is called automatically, any parent constructors need to be called explicitly.
PHP 5 uses the __construct magic method. You can use the name of the class as well, but this is only for backwards compatibility, and will throw an error if you have E_STRICT error reporting enabled (which you should have).
Instantiating a Dog requires us to specify it’s breed. The property breed is assigned a new value by cause of this initialization (previously NULL), and we have a Dog which initial state specifies it’s breed:
We could change its breed afterwards if we’d like:
2.4 Scope Resolution Operator
Officially called Paamayim Nekudotayim (Hebrew for double colon, now you know what the parser is talking about when you get errors), the scope resolution operator (::) allows you to perform static calls to methods and class members.
Static methods can be called without instantiation of an object. This can be useful both inside a class declaration (within an object hierarchy) as outside of it.
After an object is created, some reference to the overwritten methods and properties remains intact. One can access these methods from within the object statically (no new instantiation is required, yet the object from within you are operating is still affected.).
Dog is using its parent’s method eat to refer to the declaration of eat that was overwritten by its own declaration of eat. You need not specify the specific class name like in the above example to refer to the last declaration of a method (or property), the parent keyword will point to that reference:
Should you have multiple levels of inheritance, and want to address a declaration other than the one last defined, you’ll have to specify the class like in the above example class.
The constructor of parent classes is called upon instantiation, unless it is overridden. We override __construct(), so if we desire to run a parent constructor, we have to call it explicitly:
Trying to call a method statically which has references to object specific variables (makes use of $this) from outside of the object, will throw an error:
Result:
Fatal error: Using $this when not in object context
PHP 5 features static class members. It requires you to define what methods and properties can be used statically. Static properties work the same as regular static variables, if you are unfamiliar with static variables, have a thorough read on Variable Scope in the manual.
Note the self keyword. It is simply referring to this class declaration. Like with the parent keyword, alternatively one could use the actual name of the class (Dog).
2.5 Abstract Classes
Abstract classes are, well, abstract. An abstract class isn’t intended to be instantiated, but to serve as a parent to other classes, partly dictating how they should behave. Abstract classes can have abstract methods, these are required in the child classes.
Abstract classes can be used to enforce a certain interface.
An example:
