The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
> PHP FAQs and Stickies
|
Page 2 -
An Introduction to Object Oriented Programming
Page 2 - Discuss An Introduction to Object Oriented Programming in the PHP FAQs and Stickies forum on Dev Shed. An Introduction to Object Oriented Programming Look here for FAQ's and stickies on common PHP questions.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 12th, 2012, 03:56 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
Hello Northie,
Everyone has a different style of teaching, I get that. And that's cool.
Again, there are always more ways to look at the same thing.
All I'm providing is yet another take.
Your right, if some "get" it from reading your article that's great. If some "get" it from my perspective that's great too right?
So its not a case of "making it simpler", its a case of presenting it from a different angle.
We cool?
"do you catch my thought stream?"? 
|

April 12th, 2012, 05:07 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | Originally Posted by Northie If we're going to get pedantic, then I would suggest adding a pre-requisite list to the top of the first post (something I can no longer edit?!?!?) that reads something like
Pre-requisites
- You currently write and understand PHP sufficiently well to know what variables and functions are in PHP
- You are aware that PHP supports something called "objects" and that there is a concept called "Object Oriented Programming (OOP)"
This post will help you build on your current understanding of PHP towards understanding what objects are and how they are used in a PHP programming environment |
Added.
Users can only edit posts within 30 days. So if there's anything else you'd like to add, now or later, say something in here or just PM me directly.
|

April 14th, 2012, 08:38 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
|
OOP A Generic Approach
Note:
Quote: This article is taken from The IT Ninja.
It is a more generalised explanation. The hope is that once you understand the general concept, it is then very easy to apply to any language.
I hope this is a good addition to Northie's in depth (and PHP focused) explanation. |
Object Oriented Programming OOP
What is Object Oriented Programming?
This is a very good question, one that some beginners find difficult to understand.
The reason why it may be difficult to understand is that it requires to wrap your head around a level of abstraction or "concept" that some are not yet familiar with.
Sometimes, this is compounded further. Those who try to explain what it is sometimes use analogies and metaphors that don't really help because they require you to understand the abstraction in the first place!
In order to alleviate this situation, I am going to try and explain what these "concepts" are from a different perspective.
I'll try and build up from analogies that naturally make sense. So lets start of with a "standard explanation"
Quote: | Originally Posted by http://en.wikipedia.org
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance.
|
Ok, lets back up a bit, lets start off with variables:
Variables
I'm assuming you know at least what a variable is. If you do not, please go and learn what a variable is then come back.
Quote: | Originally Posted by variables in a nutshell
what is a variable?
A variable, is simply a "place-holder" like say an empty jar. This empty jar can hold things. However we can also have lables on these "jars" so that we can refer to them as "Paul's jar" or "Andy's jar".
Now these jars can only hold one TYPE of stuff, example a "peanut jar" can only contain peanuts. A "jam jar" can only contain jam.
(EDIT:
This is true in many languages, but not in PHP. PHP is loosley typed which means you can change the type of a variable or assign a new value of a different type to the variable.)
Now imagine instead of calling these jars, we call them var's (short for variables), you now understand variables!
In short, variables can hold a certain TYPE of thing and can be named as we wish.
Example, lets call "my_current_account_total" and lets make it a "number" type.
As you can see I have a "number jar" (a variable), this "number jar" has a label on it that reads "$my_current_account_total"
Now I want to put £500 in it, I do this like so:
Code:
$my_current_account_total = 500;
Wow! now my "number jar" contains £500. But now I need to pay £300 to a friend, that leaves me with £200. How do I do this?
Code:
$my_current_account_total = ($my_current_account_total - 300);
or simply:
Code:
$my_current_account_total = 200;
|
Multiple Variables
Now its time for "Jar-ception".
As we know variables have certain types, numbers, string, etc. And we can only put one type of thing in one jar at any one time.
But sometimes , I don't want to JUST put peanuts in, I might want to put say marbles also!
How can we do this?
Well one cool solution would be to get a MASSIVE Jar, and have its type as jars!!
That's right a jar that holds jars! impressive hey?
Now of course, before we go use this crazy new jar, we need to know what KIND of jars in can hold.
To do this we can write it down, on a piece of paper. We can describe it by simply listing what it can hold:
This Jar Contains these types of "jars":
* A Peanut Jar called "Andy's Jar"
* A Milk Jar called "Simon's Jar"
* A Marbles Jar called "Paul's Jar"
Excellent, the above List ONLY describes what kind of jars "this" jar can hold. Instead of calling it a description, lets call this a "Class", i.e. its a "Class" of jar that holds the above.
In a nutshell, a "Class" is a description of a type of "jar" that holds a "collection" of other jars inside it.
That's about 50% of OOP.
(EDIT:]In PHP a "class" is so much more than an array.
Following the analogies of jars, a PHP class is more like recipie, a cook and a bunch or jars: the recipie contains instrcutions (methods/functions) on how to use what's in the jars. The cook can also put things into the jars or take them out (setters and getters). This will be expanded in the part II)
|

April 14th, 2012, 08:39 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
Object Oriented Programming Part II
Recap:
In part One we talked about "Classes". A Class is nothing more than a description about a thing, it does this by listing a collection of types (or attributes).
In other words a "Class" is just a list or collection of data types. This is easy to demonstrate, when we think about things in the real world, we naturally think about its attributes.
Example:
If we imagine a "Human" Class what attributes can we list? and what would each attribute's data type be?
Code:
Class Human:
Attribute: Name, Type: string
Attribute: Age, Type: Date
Attribute: Height: Type: Number
Objects
What are objects? this is a critical juncture, where I think a lot of beginners get trapped, or worse misunderstand it.
I'm going to try and explain it in a slightly different manner than the standard definition:
Quote: | Originally Posted by Standard Definition An Object is simply an instantiated "instance" of an Class. |
I want you to imagine our "Human" class that we described above. Now I want you to put this into an spread sheet. So your spread sheet should look like the following:
Code:
Name | Age | Height
-------------------------------
| |
-------------------------------
As you can see the the "Class" attributes have become the table's header.
Now lets create and add an "Object" of this "Human" Class:
Code:
Name | Age | Height
-------------------------------
Ben | 19 | 162
-------------------------------
We have just "instantiated" (or created) an "object" that is an "instance" (or type of) "Human" Class.
In other words, objects are nothing more than "Records" in a table! in fact, just think of a class as a "table"
Methods
What are Class methods? Well Classes are simply not JUST a collection of attributes (columns in a table).
We can add functions (blocks of code), that can manipulative those attributes to do useful things.
For example, we could add a method that creates a greeting message based on the attributes of that class.
Code:
Class Human:
Attribute: Name, Type: string
Attribute: Age, Type: Date
Attribute: Height: Type: Number
Method: Greet Message,
"Hello! my name is: [Name], and I am [Age] years Old!"
Encapsulation
Encapsulation in a nutshell is simply data hiding. This means there may be attributes of a class that we want to hide. Objects (records) will have certain attributes hidden, however only the object (record) will be able to access this hidden data.
Example:
Code:
Class Human:
PUBLIC
Attribute: Name, Type: string
Attribute: Age, Type: Date
Attribute: Height: Type: Number
Attribute: Message, Type: string
Method: Greet Message,
"Hello! my name is: [Name], and I am [Age] years Old!"
Method: Send Email,
Send [Message] to [Email]
PRIVATE:
Attribute: Email, Type: string
In the above example, we have added a "Private" attribute called email, and a "Public" attribute called "Message". We have also added a new function called "Send Email". This takes the message and emails it to the objects (records) hidden email field.
Inheritance
This is really one of the most powerful aspects of OOP. As the name suggests, when we define a class we can also create another class that "Inherits" from the first class.
The first class can be thought of as the "Parent Class" and the inherited class as the "Child Class".
The child class, will have everything the same as the parent class, however we can add new things to the child class that does not exist in the parent class (New attributes, new functions).
Also we can have many children classes, and each one will have some things in common, but at the same time each child class will have its own sets of things unique to itself.
Some children classes can even have more children classes and so on. This creates a "hierarchy". When we make a change in the "Parent Class", these changes effects ALL its children classes and the changes automatically ripple through.
Example:
We know what a vehicles as a concept is. We also know that all vehicles have the following attributes:
Code:
* Weight
* Engine Size
So we can create a "Parent Class" called vehicles with the above attributes. From this parent type, we can create child classes:
Code:
* Boat
* Plane
* Car
* Truck
* Motorbike
Each one of these child classes will have different attributes, for example cars and trucks will have "Number of wheels", while a boat may have "number of propellers".
However they will all share the same attributes that the parent class has. Now we realise that all vehicles, have another thing in common: Fuel Type!
We can simply add this to our parent class, and all the children classes will also automatically have this attribute.
|

April 16th, 2012, 03:23 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
@Darknite: Thank you for yor input
Unfortunatly your post is far too generic and goes on to say say things that are just not true to PHP. As this is a PHP forum where lots of people new to the language come to learn I feel that your post could be just too confusing/misleading. I will now correct highlight specific cases for PHP
Quote: | Originally Posted by Darknite Now these jars can only hold one TYPE of stuff, example a "peanut jar" can only contain peanuts. A "jam jar" can only contain jam. |
This is true in many languages, but not in PHP. PHP is loosley typed which means you can change the type of a variable or assign a new value of a different type to the variable.
PHP has very few type constraints: Constants may only contain scalr data, the type opperator === or !== is used to match type and value
Quote: | Originally Posted by Darknite
my_current_account_total = 500
Wow! now my "number jar" contains £500. But now I need to pay £300 to a friend, that leaves me with £200. How do I do this?
my_current_account_total = (my_current_account_total - 300)
|
in php this would be written as
PHP Code:
//
$my_current_account_total = 500;
$my_current_account_total = $my_current_account_total - 300;
Quote: | Originally Posted by Darknite
As we know variables have certain types, numbers, string, etc. And we can only put one type of thing in one jar at any one time. |
Compleley wrong and misleading when it comes to PHP
Any variable can hold any type at any time during the execution of your script
Quote: | Originally Posted by Darknite
This Jar Contains these types of "jars":
* A Peanut Jar called "Andy's Jar"
* A Milk Jar called "Simon's Jar"
* A Marbles Jar called "Paul's Jar"
Excellent, the above List ONLY describes what kind of jars "this" jar can hold. Instead of calling it a description, lets call this a "Class", i.e. its a "Class" of jar that holds the above.
|
Again, missing a trick here
PHP has arrays - an array is a variable with many elements. each element can hold any variable, of any type (inlcuding other arrays)
Arrays in php have nothing to do with classes or objects
Quote: | Originally Posted by Darknite In a nutshell, a "Class" is a description of a type of "jar" that holds a "collection" of other jars inside it.
That's about 50% of OOP. |
Although this sentence is techically correct what you described as a "class" in PHP is actually an array. in PHP a "class" is so much more than an array.
Following your analogies of jars, a PHP class is more like recipie, a cook and a bunch or jars: the recipie contains instrcutions (methods/functions) on how to use what's in the jars. The cook can also put things into the jars or take them out (setters and getters)
Quote: | Originally Posted by Darknite In other words a "Class" is just a list or collection of data types. This is easy to demonstrate, when we think about things in the real world, we naturally think about its attributes. |
....and methods
|

April 16th, 2012, 05:15 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
Hello Northie,
Those are all valid points and I completely agree with you, however it is a matter of context.
When looked from a PHP specific perspective, or Python or Ruby then there will be differences.
It is for this reason I had already stated:
Quote: It is a more generalised explanation. The hope is that once you understand the general concept, it is then very easy to apply to any language. |
Your original article already deals with the PHP specifics. I have only provided the generalised concept. Together I believe they work well.
|

April 16th, 2012, 06:53 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Darknite, I can appreciate what you're saying. There's more to OOP than just PHP's implementation. But this is the PHP forum. All we're concerned about here is how you do stuff with PHP.
I agree with what Northie's said. You are free to add your own guide here - please do - but err on the side of saying things that are specific to PHP rather than how other programming languages work. Can you edit your posts to accommodate what Northie's pointed out?
Last edited by requinix : April 16th, 2012 at 06:55 AM.
|

April 16th, 2012, 07:02 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
Quote: | Originally Posted by requinix Darknite, I can appreciate what you're saying. There's more to OOP than just PHP's implementation. But this is the PHP forum. All we're concerned about here is how you do stuff with PHP.
I agree with what Northie's said. You are free to add your own guide here - please do - but err on the side of saying things that are specific to PHP rather than how other programming languages work. Can you edit your posts to accommodate what Northie's pointed out? |
Hello requinix,
Yes this would be a good solution. I don't have time just right now to make the detailed changes.
I however do promise to incorporate Northie's corrections, when I get some time to sit down and carefully go through each change.
So, please do watch this space 
|

April 23rd, 2012, 06:45 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Location: London
|
|
|
Hello guys/gals
As promised now that I've had a chance to edit the post, I've added in Northies corrections/PHP amendments.
Cheers everyone!
darknite.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|