PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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.