View Full Version : php object oriented programming

05-17-2011, 06:43 PM

i am trying to understand how to use OOP in web developing. i read many tutorials but most of them was just about OOP (what classes are ..) not how to use OOP in web developing. i know how to use it, i know about variable types, methods, interfaces, abstract classes, inheritance.... just can't get the idea how to use it for web development.

in PC games I can imagine OOP better. for example, some car is an object of a class car that have variables like model, color, max speed and methods like start engine, accelerate, brake, stop engine. but where do i see objects in web pages?

i just need some good examples of simple OP web pages and then i will understand it i hope. can someone upload me some example web page or at least post a link to a tutorial, that can help me? i would really appreciate it. thanks

05-17-2011, 07:04 PM
OOP is simply a concept change from procedural. Anything that can be done in OOP can be done with less work and faster in procedural code. The advantage of OOP is its incredible added control, dynamic and static binding, and reusability. Best part: centralized management.
Frankly, PHP is a poor choice to learn OOP on, simply because of its weak datatype controls. I would actually look at using Java instead to learn on; no option but to use it in an OO fashion, and PHP 5.x's engine appears to be quite closely related to the OOP of the JDK (Zend appears to have stolen most of the features from Java for its OO core, which is super great IMO).

Its really a matter if seeing it as you have described. Its no different if its a web page or a desktop app or a mobile or whatever.
You can look at a page and see: I have a menu, on that many are links. I have a section for articles and articles within that section posted by users.
Just that would say: Menu is a collection of Link objects. Articles are objects within a collection for Sections. User own Article. So that would be 5 classes.

Start with a database (or filestructure or whatever) design. This is the most important step in the design process. Bad structures always cause bad code to make up for the deficiencies within the structure itself. I myself tend to focus one table to one class representation, but that is mostly due to the n-tier crud I've written to deal with them that way, and its simply easier to continue using it than to create a new one. So typically for each table I have, I will have 2 or 3 classes: 1 to represent the object, 1 to control datastore, and maybe 1 to represent a collection of this type if its necessary (more often than not).

Start with java, and focus on even just CLI interaction. GUI is IMO useless if you plan to use it only to learn the OO functionality (and the GUI skills are not really transferable to other languages; even C#'s GUI handling is quite a bit different than Java's for languages that are otherwise very very similar). Practice is all you need in order to see what you need to do for separating data into different objects.

I should mention as well, the complexity of course increases as you're desire for dynamic data increases. With the above example I had, if you have a menu that is always only static links, it may be a waste to have a link and menu class (IMO its never a waste, remember reusability). These are the things you need to make decisions about - for every pro you take you receive a con to go with it. Its all about the tradeoffs.

05-17-2011, 11:52 PM
I tried Java and C++ a few days ago but only a syntax and read something about memory. it isn't very different from PHP at these elementary things.

ok then, i try to learn OOP in Java first.

but anyway, do you know some downloadable simple OO web pages? it would help me a lot. learning by examples is much faster for me than reading guides. all PHP I know I have learned this way - I have downloaded some code and "played" with it until I learned the basic. then I have read some guides to know details.

unfortunately, it doens't work in OOP, because every guide i read contained 90% theory and 10% examples. I just can't find any simple downloadable OO web page (desktop app or something in Java.....) to play with and try things on.

btw, sorry for my English, I am trying really hard :)

05-18-2011, 01:18 AM
I can understand why it covers mostly principles; these are transferable while the examples are generally useless in real life.
Wait for someone that has more experience with third party software. I can't remember the last time I didn't build something I needed. I *think* CMS systems like Joomla are object oriented (minus at least one procedural call since PHP isn't truly object oriented), but I can't be sure.