Skip to content

Object Oriented Programming

Andy Theuninck edited this page Feb 13, 2015 · 2 revisions

CORE uses object oriented programming extensively. This is a gigantic topic but Wikipedia isn't bad for a free reference. PHP's specific syntax may also be helpful. Concepts of particular importance are:

Classes and Objects

A class is a defined type packaging together both variables and functions into once cohesive unit. Variables are often referred to as "properties" in OOP and functions are often referred to as "methods". For most practical purposes, each pair of terms is interchangeable. Objects are individual instances of classes. Just as you can create multiple different variables of type integer or string and assign them different values, you can create multiple objects of the same class.

Inheritance

Classes can be structured in a hierarchy. A child class automatically contains the properties and methods defined in the parent class. If a class "A" has property $foo and method Bar(), then a child class "B" that inherits from "A" will have the same property and method available. The child class "B" may provide its own method named Bar() which overrides the method inherited from "A", but both Bar() methods must accept the same arguments and return the same type. Inheritance can be used for sharing functionality as well as for providing a common interface.

Polymorphism

Consider a function

function polymorph($object)
{
  if ($object->Bar()) {
    return true;
  } else {
    return false;
  }
}

Continuing from the inheritance example, it doesn't matter if the object's type is class "A" or class "B". Because the two classes provide the same set (or at least subset) of methods, other code can use instances of those classes interchangeably. This is extremely helpful for modularity. Different classes from the same hierarchy can be swapped in and out based on configuration.

Encapsulation

This is more of a minor topic, but it's worth understanding visibility setting for properties and methods.

  • public properties and methods can be accessed from anywhere
  • private properties and methods can only be accessed from within the class where they are defined
  • protected properties and methods can be accessed from the class where they are defined as well as that class' children In PHP if none of these keywords is specified a property or method is public.

Static Methods and Properties

Many languages, including PHP, allow for properties and methods to be attached directly to a class. These are like pseudo-global variables and functions in that they can be referenced without an instantiated object.

PHP-Specific Considerations

Unlike a lot of languages, PHP does not have a formal, built-in package system. When you reference a class or function, you have to keep track of where it's defined and make sure that definition has been included. PHP does have a mechanism where registered handlers can load classes automatically. Many projects, including CORE, will wrap just about everything in classes to take advantage of this.

CORE does not make much use of interfaces or abstract classes. That's a perhaps misguided attempt to keep the OOP learning curve under control. CORE also does not use namespaces much but that's gradually shifting. It's not a high priority though.

Clone this wiki locally