Categories
PHP PHP 5 Web Development

PHP & OOP: A Fundemental Tutorial on Object Oriented Programming and PHP

PHP & Object Oriented Programming

PHP can be used both procedurally and with an object oriented approach.

In PHP & procedural programming the emphasis lies with the actions and the steps that must be taken to perform actions such as submitting a form or retrieving a record from a database.

In PHP & Object Oriented Programming the emphasis lies on the objects and the analysis and definition of the kind of things each object will have (attributes) and do (functions and procedures).

php &

 

PHP & the Class

The core concept behind PHP & OOP is the class. A class is the blueprint or template defining an object: the information is needs to have and the common actions it does. To represent an HTML page as a class: attributes would be title, content, date created and date last updated. Actions would be remove HTML tags or show Preview.

With that in mind the HTMLpage class would have attributes [title, content, datecreated, datemodified] and methods [stripHTML(), showPreview()].

PHP & the Object

Once you have created the class blueprint, we can now create objects baed on the class. So Home page an object representing an HTMLpage, About Us as object representing an HTMLpage and Cool Stuff an object respresenting an HTMLpage. Home Page, About Us and Cool Stuff would all have the same attributes and methods within themselves (objects). However, the content within each page would be different ie. about us would have about us content and home page would have homepage content.

A good class makes projects more reliable and easier to maintain. A proper PHP & OOP philosophy requires a good amount of theory and design as opposed to a procedural which writes itself as a logical flow.

Bad PHP & procedural programming tends not to work well but can be easily treated, bad PHP & OOP programming is a complicated mess which can be a real chore to fix. Good PHP & OOP is easy to extend and reuse.

PHP & Concepts

PHP & Modularity

Modularity is breaking function down into more manageable bite sized chunks.

PHP & Encapsulation

Encapsulation means how something works and how it is used is separated. For example stripping a page of HTML, you would not need to know how a method does that, as all you need to do is use it.

This concept goes hand in hand with access control (visibility). Proper access control improves an application’s security and reduces the risk of bugs. There are 3 levels: Public, protected and private.

PHP & Inheritance

One class can be defined as an extension of another, setting up a parent-child relationship (base-sub class). The child class can inherit members of the parent class. A public member can be accessed anywhere within the class, derived class or through object instances of those classes. A member defined as protected can only be accessed within the class or within derived classes but not through object instances. A attribute marked private can only be accessed within the class itself, not derived classes.

PHP & Abstraction

Ideally base (parent) classes should be as generic as possible, specific functionality should come when child (sub classes) extend from them. Inherited classes will inherit all public and protected members and can define its own. eg. A person class has eat() and sleep() functions. An adult inherited class will have eat() and sleep() and work() as an added abstracted member method.

Note: PHP does not allow a single child class to inherit from multiple parents.

PHP & Polymorphism

Child classes can also override a parent class’s method. It does this by redefining what that method does, this is known as polymorphism.

PHP & Syntax

Class:

class ClassName
{
}

Class with attribute and function (with visibility indicators):

class ClassName
{
public $variable;
public function thisFunctionName(){
//function internals
}
}

Note: public is the default visibility, but it is best practise to be explicit.

Calling a class function:

ClassName::thisFunctionName()

Creating an instance of Class (object):

$object = new ClassName();

Referencing public attributes and methods:

echo $object->variable;
$object->thisFunctionName();

Referencing attributes and methods within the class:

Class ClassName
{
public $variable;
public function thisFunctionName(){
$this->variable = 50;
return $this->variable;
}
}

Constructors and Destructors:

function _construct
{
}

function _destruct()
{
}

A constructor often takes arguments (parameters) but cannot return any values. A destructor cannot take arguments.

Inheritance

class ChildClass extends ParentClass
{
}

Inheritance is indicated using the extend keyword.

PHP & Naming Conventions

The best practices for naming is as follows:

  • ClassName: Upper-CamelCase (ClassName, ChildName)
  • methods and attributes: lower-CamelCase (doThis() or someVar, fullName)
  • Private Attributes: underscore in front (_variable)

Source: Larry Ullman, Yii Book Introduction

Please Support him and buy the book if you are interested in the Yii Framework