"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'.
Variables that hold objects (instances of a class) are not primitive data types; they are reference data types.
The difference between primitive and reference data types lays in the fact a computer stores them in memory diversely.
Without going into too many details, the main difference is that for primitive data types the value assigned is stored directly in a block (or cell) of memory, while for reference data types the memory block simply holds an address that is a reference to the instance of a class.
I am going to conclude this article with a simple code example on how to define a class and create an instance of that class in PHP.
// class definition for Daddy
// code for Daddy class goes here
// declare $myDaddy and assign to it instance of Daddy class
$myDaddy = new Daddy();
The code won't do anything so don't try it. What you need to focus on is simply how to create a template for an object via the keyword class and how you can create new objects of a class via the keyword new.
Note also the class definition has no round brackets while when creating an object we use round brackets after the name of the class. There is a reason for this and will be the subject of another article. Thanks for reading!
|PHP Classes And Objects: A Guide To Development|
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
|PHP and OOP|
Categories : PHP, Object Oriented, Beginner Guides
|PHP 101 Part 7 of 15 : The Bear Necessities|
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
|How To add paging (Pagination) with PHP and MySQL|
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
|Beginners Guide to PHP - Introduction to cookies|
Categories : Beginner Guides, Cookies, To PHP, PHP
|PHP 101 Part 8 of 15 : Databases and Other Animals|
Categories : PHP, Beginner Guides, Databases
|Making PHP Forms Object-Oriented|
Categories : PHP, HTML and PHP, Object Oriented
|PHP 101 Part 9 of 15 : SQLite My Fire!|
Categories : PHP, Beginner Guides, Databases, SQLite
|Beginners guide to PHP and MySQL|
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
|Counting - Creating a GIF based counter using PHP and MySQL|
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
|How TO Install PHP, Apache and MySQL on Linux or Unix|
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
|Counting - Creating a more sophisticated GIF based counter using PHP and MySQL|
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
|PHP 101 Part 10 of 15 : A Session In The Cookie Jar|
Categories : PHP, Beginner Guides, Cookies, Sessions
Categories : PHP, Beginner Guides, To PHP
|Building An Extensible Menu Class|
Categories : PHP, PHP Classes, Object Oriented, Navigation