PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 27, 2020 9:21 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: OOP example for beginner
PostPosted: Tue Dec 29, 2009 7:55 pm 
Offline
Forum Newbie

Joined: Wed Dec 23, 2009 10:45 am
Posts: 3
Hi,
I am learning OOP in PHP and the basic rules i know, but i fail to prove, how the application does work as complex. I try looking for example a guestbook, public inquiry and similar simply code in OOP, but no result.

That's why I shall be happy to, whether me somehow we help with this problem.

Thanks for your time :wink:


Top
 Profile  
 
PostPosted: Tue Dec 29, 2009 10:17 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
First you must acknowledge structured programming, the fact that code should not run top to bottom, but should use functions to separate concerns (if dealing with a complex system). For an example look at Drupal or Mantis BT, both of these projects broke down the logic to the sub-routine (function) level.

OOP groups functions into objects where functions can store data. This gives data scope so you don't have to add too many function arguments or resort to using global variables. These advancements were made in the early 1970s and considered fundamental.

There are 4 corner stones to OOP. For instance encapsulation means objects adhere to a common interface. If you have a $creditCard object of type CreditCard_MasterCard and a $creditCard2 of type CreditCard_Visa, you should be able to do $creditCard->charge( $theNumber ). So the concept of encapsulation is that the "user level code" (in this case the checkout system of an e-commerce shopping card, does not need to know which type of credit card is being used, that behavior is "encapsulated". The credit card could be changed without changing the checkout page.

Polymorphism follows, and means we can add more credit cards or payment methods as long as they implement the charge() method the same way. We tell the credit card to charge and it charges, we don't need to worry about which payment processor it is talking to because that aspect is "encapsulated"

There is a lot more to OOP and I encourage you to read up on it. The problems that are solved by it are code that grows complex over time, with nested conditionals, use of global variables, and use of complex code like if statements and loops just to do something simple like render a button. You see maybe later on the client wants different product types and a different kind of button will be needed. So in OOP we encapsulate things that we anticipate may need to be changed or added on to. Sort of like a "plugin" system that firefox has. They can add new search engines to the "quick search" without having to change any of the "core" code. They simply add a new "plugin".

Another fundamental thing is you can have "multiple objects". Lets say you finish the application and your boss says great, now I want to have it running 5 windows (like firefox). Well if you used global data, the windows would clash with eachother and overwrite each other's data. Objects keep your program structured in a way that problems like these vanish from your every day life.


Top
 Profile  
 
PostPosted: Tue Dec 29, 2009 10:24 pm 
Offline
Forum Regular
User avatar

Joined: Tue Jul 15, 2008 10:50 pm
Posts: 738
Location: Carolina, USA


Top
 Profile  
 
PostPosted: Wed Dec 30, 2009 3:31 am 
Offline
Forum Newbie

Joined: Wed Dec 23, 2009 10:45 am
Posts: 3
josh, thanks you from your extensive answer :wink:
omniuni, some simillar example i searched. I study your post posted code, also thanks :wink:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 27 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group