Everything is an object

Bookmark on del.icio.us

“We are all made of stars” sings Moby in one of his top-selling tracks, and how could I disagree with that?!

If there are about 7,000,000,000,000,000,000,000,000,000 atoms in a 70kg human body, and if we would consider atoms as objects, is everything an object, even us?

Trying to give an answer to this question can start a pretty long moral debate, but I can affirm surely that that’s the way it is in object-oriented programming.

Thanks to the progress of abstraction, in OOP it is possible to take conceptually any real-life component and build a software representation of it.

For example, you can create a Dog object, a Car object, or you might as well create a Daddy object and so on.

Each one of these objects has in its basic definition both data and behaviour, being data in the form of attributes’ values, and behaviour the actions it takes when receiving a particular request.

Having both data and behaviour is the key difference between object-oriented programming and the procedural approach. In well-designed object-oriented software, nothing outside the object can change directly the value of its attributes, and the only way to communicate with the object, or have it doing anything, is to send it a message.

We could at this point narrow down the definition of an OOP application as being a collection of objects that communicate with each other by sending messages.

A message is a request you send to an object in order to gain, give or process data.

When we say “send a message” to an object, what happens in reality is we make a call to a method which is part of the behaviour of that object.

The set of all the methods part of the definition of an object is often called ‘protocol’, and the collection of all the values’ attributes of an object is often referred to as ‘state’.

So we can say that an object has a state and a protocol, both of which had been defined in its abstract software representation.

Basic definitions (or abstract software representations) for an object in PHP (as well as in other languages) are provided via the keyword class.

The keyword class is not actually used to create objects ( this is achieved in another way), but to define the main template from which objects belonging to that class are created.

This makes perfect sense. Think about if you had to provide different code for any Dog object you need just because they have different names or because one is black and the other white.

Instead, thanks to the class keyword, we can define a software representation of a dog, with certain attributes and behaviour and create as many Dog objects as we want.

Each time we create a new Dog object we can give it different attributes values (e.g Fufi, black, etc) as well as being able to send to the object messages part of its protocol. Creation of new objects is achieved via the keyword new and we’d say that a new instance of that class has been created.

When defining a new class, we bring in a new type; this means we will be able later on to define a variable of that type, the same – or better, almost the same – as we do with numbers, strings and booleans.

You may know common data type such as an integers, double numbers, etc. These are data types available by default to a programming language and often called ‘primitive data types’.

For example, you can create a Dog object, a Car object, or you might as well create a Daddy object and so on.

Each one of these objects has in its basic definition both data and behaviour, being data in the form of attributes’ values, and behaviour the actions it takes when receiving a particular request.

Having both data and behaviour is the key difference between object-oriented programming and the procedural approach. In well-designed object-oriented software, nothing outside the object can change directly the value of its attributes, and the only way to communicate with the object, or have it doing anything, is to send it a message.

We could at this point narrow down the definition of an OOP application as being a collection of objects that communicate with each other by sending messages.

A message is a request you send to an object in order to gain, give or process data.

When we say “send a message” to an object, what happens in reality is we make a call to a method which is part of the behaviour of that object.

The set of all the methods part of the definition of an object is often called ‘protocol’, and the collection of all the values’ attributes of an object is often referred to as ‘state’.

So we can say that an object has a state and a protocol, both of which had been defined in its abstract software representation.

Basic definitions (or abstract software representations) for an object in PHP (as well as in other languages) are provided via the keyword class.

The keyword class is not actually used to create objects ( this is achieved in another way), but to define the main template from which objects belonging to that class are created.

This makes perfect sense. Think about if you had to provide different code for any Dog object you need just because they have different names or because one is black and the other white.

Instead, thanks to the class keyword, we can define a software representation of a dog, with certain attributes and behaviour and create as many Dog objects as we want.

Each time we create a new Dog object we can give it different attributes values (e.g Fufi, black, etc) as well as being able to send to the object messages part of its protocol. Creation of new objects is achieved via the keyword new and we’d say that a new instance of that class has been created.

When defining a new class, we bring in a new type; this means we will be able later on to define a variable of that type, the same – or better, almost the same – as we do with numbers, strings and booleans.

You may know common data type such as an integers, double numbers, etc. These are data types available by default to a programming language and often called ‘primitive data types’.

  1. No comments yet.

  1. No trackbacks yet.