Helping ordinary people create extraordinary websites!

What is Object Oriented Programming (OOP)?

By Tony Marston
2007-01-30

The difference between OOP and non-OOP
A better way of trying to explain the differences between non-OO and OO programming is to actual examples.

They are defined differently

A function is defined as a self-contained block of code. Each function name "fName" must be unique within the application.

function fName ($arg1, $arg2) 
// function description
{
....

return $result;


} // fName

A class method is defined within the boundaries of a class definition. Each class name "cName" must be unique within the application. Each class may contain any number of functions (also known as "methods"), and the function name "fName" must be unique within the class but need not be unique within the application.
class cName
{
function fName ($arg1, $arg2)
// function description
{
....

return $result;

} // fName

} // cName

They are accessed differently

It is important to note that neither a function nor a class can be accessed until the function/class definition has been loaded.

Calling a function is very straightforward:

$result = fName($arg1, $arg2);

Calling a class method is not so straightforward. First it is necessary to create an instance of the class (an object), then to access the function (method) name through the object. The object name must be unique within the application.

$object = new cName; 
$result = $object->fName($arg1, $arg2);


They have different numbers of working copies

A function does not have to be instantiated before it can be accessed, therefore only one copy (or instance) is said to exist at any one time.

A class method can only be accessed after it has been instantiated into an object, and it is possible to create multiple instances (objects) of the same class with different object names.

$object1 = new cName;
$object2 = new cName;
$object3 = new cName;


They have different numbers of entry points

A function has only a single point of entry, and that is the function name itself.

An object has multiple points of entry, one for each method name.

They have different methods of maintaining state

A function by default does not have state, by which I mean that each
time that it is called it is treated as a fresh invocation and not a continuation of any previous invocation.

An object does have state, by which I mean that each time an object's method is called it acts upon the object's state as it was after the previous method call.

It is possible for both a function and a class method to use local variables, and they both operate in the same way. This means that the local variables do not exist outside the scope of the function or class method, and any values placed in them do not persist between invocations.

It is possible for a function to remember values between different invocations by declaring a variable as static, as in the following example:

function count () {
static $count = 0;
$count++;
return $count;
}


Each time this function is called it will return a value that is one greater than the previous call. Without the keyword static it would always return the value '1'.

Class variables which need to persist outside of a function (method) are declared at class level, as follows:

class calculator
{
// define class properties (member variables)
var $value;

// define class methods
function setValue ($value)
{
$this->value = $value;

return;

} // setValue

function getValue ()
// function description
{
return $this->value;

} // setValue

function add ($value)
// function description
{
$this->value = $this->value + $value;

return $this->value;

} // setValue

function subtract ($value)
// function description
{
$this->value = $this->value - $value;

return $this->value;

} // setValue

} // cName


Note that all class/object variables are referenced with the prefix $this-> as in $this->varname. Any variable which is referenced without this keyword, as in $varname, is treated as a local variable.

Note also that each instance of the class (object) maintains its own set of variables, so the contents of one object are totally independent of the contents of another object, even it is from the same class.



Tutorial pages:
 1 Votes

You might also want to check these out:


Leave a Comment on "What is Object Oriented Programming (OOP)?"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS