Winnipeg Photographer


OOP – Chapter 2 notes

PHP OOP Definitions

Coming from a procedural programming background you will be familiar with two main items.

1.) Functions
2.) Variables

In Object Orientated Programming the previous two items are called:

1.)Methods
2.)Properties.

Not all variables in a class are properties. A property is a special type of variable  that can be used anywhere inside a class and sometimes outside as well.
There are several different types of “Visibility Types”. These types are called:

1.) Public
2.) Private
3.) Protected
4.) Abstract

What is meant by “Visibility”?

Visibility determines whether a property or method can be accessed directly by any part of the script that uses a class, or whether it remains internal to the class.

PUBLIC

This means the property or method  can be accessed by any part of a script both inside and outside the class definition. All methods are regarded as public unless preceded by a different modifier.

PROTECTED

This prevents external access to a property or method, but permits access internally and to parent and child classes.

PRIVATE

Private properties and methods can be accessed only within the class they were defined.

FINAL

Final properties can NOT be extending. The buck stops here.

How do you create a new Class?

You would create an “INSTANCE” of the class. This would be done using the php “new” keyword like so:

$val = new Product();

Protecting data integrity with encapsulation

Encapsulation ensures that each part of an application is self-contained and doesn’t interfere with any others, except in a clearly defined manner. OOP breaks down complex tasks into components.

Polymorphism

This is a relatively simple concept. It applies to both methods and properties. It means using the same name in different contexts. For example. The word Head is given to the spherical shape on the top of a human body. A horse has a head as well but it’s head is made up of completely different shaped properties.

Extending classes through Inheritance

Once a wheel has been invented, there is no need to reinvent it. You can how ever improve it or adapt it for specialized uses. A child/subclass in OOP can inherit all the features of it’s parent or superclass, adapt some of them and add new ones of its own. Unlike humans who can have two parents, objects in PHP can only have one.

Constructors

Constructors are used to set the default values. An example of one may look like

public function __construct($value)
{
	$this->_property = $value;
}

FINAL and EXTENDS keywords

FINAL

The “final” keyword prevents objects and methods from being overridden. You would precede the class name or method’s “VISIBILITY: modifier with the keyword “final”. *NOTE *I said “over ridden” not re-used in a child object. Also do not be confuse FINAL with the “private” visibility modifier. A “private” class can not be accessed by another class. A final one can!!!  An example of using “final” would be:

final Ch2_Book extends Ch2_Product
{
	//whole class is defined as final
}

EXTENDS

This PHP keyword gives the programmer the ability to create a child object. Extending objects is one of the main reasons for using OOP. For example: Say you create a “product” object for a shopping cart on a website. Then you create a “Book” object that will extend all of the properties and methods you already created without having to re-type / re-program everything you would do something like this:

class Ch2_book extends Ch2_Product
{
	//Class info goes here ;) 
}

Constant “properties”

To create a property that is considered part of the object yo have to use the “const” keyword to declare a property fixed. Constant properties are not called using the “$”. They are called using either “self::” or  “Object_Name::”. * NOTE * – Constant Properties can be over ridden by child objects! So if you want some thing that is really a constant you must use “define” which makes a “GLOBAL” constant. GLOBAL constants how ever are much slower then Constant properties.

Example of a Constant Property:

class Ch2_Product
{
	const POUND_TO_KILOGRAMS = 0.454;
	public function lb_to_kg_Converter($poundsEntered)
	{
		return self::POUND_TO_KILOGRAMS * $poundsEntered;
	}//End lb_to_kg_Converter
} // End Ch2_Product

STATIC properties and methods

STATIC Properties

Static properties are similar to constants in that it is “Owned by the WHOLE class, and not an instance of the class”.

Click her for an Example of PHP SATIC properties in action.

Advanced OOP features

There are two main items that are considered more advanced OOP topics in PHP. These are “abstract classes” and Interfaces. There are other items like “Type Hinting”, “Magic Methods”, and “Destructors” that I will also cover.

Abstract Class

In short: An abstract class defines the basic nature of the its child classes but CAN NOT be instantiated on its own.

Interface

In short: An interface is a LIST of methods that must be implemented. Unlike an Abstract class an Interface will NEVER have properties and CAN NOT define how the methods are implemented. Interfaces can help simulate multiple inheritance in PHP. In PHP a class can only have one parent but it CAN have multiple Interfaces.


Leave a Reply