1. Object Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a type of programming added to php5,With the release of php5, php programmers finally had the power to code with the ‘big boys’. Like Java and C#, php finally has a complete OOP infrastructure.

Before we move onto writing object-oriented code in PHP, it’s important to understand 4 fundamental OOP concepts: classes, objects, properties, and methods.

A class is a blueprint for objects. When you build your object-oriented application, you typically create one or more classes representing various types of entities in your app.For example, if you’re writing a forum application, you might create classes called Forum, Topic, Post, and Member.


You define your own class by starting with the keyword ‘class’ followed by the name
you want to give your new class.

<?php class person { } ?>

This class doesn’t do much until you’ve added properties and methods to the class.
Nevertheless, the above code creates a valid, usable class in PHP.

[ It’s good programming practice to put each class’s code in its own file, with the same name as the class.For example, you might put the above Member class code in a file called Member.php, and store the file in a folder called classes. ]

An object is a special type of variable that is created from a class.It contains actual data, and you can call the object’s functions to do stuff to that data.You can create as many objects as you like from a single class.Each object functions independently of the others, even if they all come from the same class.

To use a real-world analogy:
. A class is like a blueprint for a car. It defines how cars created from the blueprint will look and behave, but it’s still an abstract concept.
. An object is like a real car created from the blueprint. It has real properties (such as how fast it’s going), and real behaviors (like “accelerate” and “brake”).


How to create objects in PHP:
You create an object from a class by using the new keyword, as follows:

<?php $stefan = new person(); ?>

Class member variables are called “properties”.They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.This declaration may include an initialization,but this initialization must be a constant value–that is,it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property).
Static properties are accessed by using the :: (Double Colon): self::$property.
Scope Resolution Operator (::) (also called Paamayim Nekudotayim)

The ‘$this’ variable:
You probably noticed this line of code: $this->name = $new_name The $this is a built-in pseudo variable (built into all objects) which points to the current object. Or in other words, $this is a special self-referencing variable. You use $this to access properties and to call other methods of the current class.

The functions that are defined within a class — and used in an object — are known as methods.In many ways, they’re just like regular functions — you can pass values to them,they can contain local variables, and they can return values.





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s