![]() |
PHP OOP - Am I the only one?
Who finds it terribly perplexing :P
I've read god knows how many online tutorials and explanations, and whilst the theory makes sense, when I sit down to do something as simple as connect to a database I manage to get myself in a muddle. Procedural stuff is simpler for me to understand but I just can't get my head around the whole OOP thing. Back to practicing ... I was just curious if I was being particularly stupid! |
It is common for people not to understand OOP with PHP. I can't really relate though because I understood OOP before I ever started PHP.
|
Which part do you need help with?
|
Try working with java or c#.net. You'll be forced to use OOP.
|
I like to see OOP as a way of thinking, even in java you can write procedural code. It takes practice but it will click eventually.
I find designing is as important as building the actual app, try drawing a few relational scribbles of the objects and work out in you’re mind how you think it should work. This should help you grasp the concept of objects. UML is also a great way to learn good OOP design. |
I don't know. I found it extremely simple, to use classes. Took me like 1-2 weeks to get the hang of it. Ofcourse you need to practice to do more complex things.
I watched video tutorial from jonathan sampson, try search for him on google! Extremely good tutorials where he talks and explains classes(again; only the basics, but once you understand that, you can understand the more advanced OOP tutorials that are written, and not filmed). |
I also had a lot of trouble understand OOP, even after reading several tutorials. I only really started to understand how classes worked after I wrote one.
I suggest you get stuck in with something simple, perhaps something that simply adds a couple strings together then outputs it, just to get a feel for it. |
Inheritance is what finally made it all click for me. Write a base class such as a user class then a class such as admin and a class for a regular user (just an example) then make those two classes inherit functions from the main user class. Once this concept finally clicked it all just became easy.
|
Learn Java, and I garuntee that you will learn OOP by the time you're used to Java.
Since I came to PHP after I did Java, I was basically used to OOP in PHP. You'll get used to it soon, don't worry! Here's a simple task you can do with OOP, and address book. |
The problem I've always found with most OOP tutorials if that the examples are practically useless. OOP is really of no advantage whatsoever for small scripts, which are all that can be covered in small tutorials. OOP begins to make sense when you begin designing large scripts with big portions of code that must be reused.
|
Quote:
|
I am also having trouble writing OOP. I fully understand all the rules and I know what everything means/does.
But when I am wanting to write some useful application, I get stuck. Maybe it's a good idea on a few people giving us some (basic) exercises we have to write OOP. Then discuss it later. The level should go from basic to advance within a weeks? For example; first we have to write a basic Database class with a few (by you defined methods) in it. Later on we will cover inheritance etc. Please reply on this post! Edit: Also useful exercises are to write a class which gets like all products from a database and lists them on a page. The thing I struggle with is that I don't know whether to make an object for every product or not. |
you dont really have to use oop the whole purpose of it is to reuse code on multiple pages rather than having to retype a bunch of things.
OOP is just a bunch of functions places all together and work together. here is an example i kind of forgot how its written but ill try and explain Code:
Class phpConnect {Code:
as for me, this whole time that i've been doing php i havent used classes yet but i should because i need to create a few tools with classes so all i need to do is type one line of code the next time and all my hours spent on making something will be cut down to 1 minute!. |
In the previous example it is more work but say you have to connect to 10 or even 50 different databases. You would modify the class just a little to be something where you just declare an instance with
$con1 = new phpConnect("dbname1"); that code would be much shorter and easier to manage then 50 sepereate procedural declarations. One benefit of OOP is that it also allows you to more easily reuse code. So on your next project you just take the db class from your last project and you already have that chunk of code written. |
I am trying to learn OOP as well. Village Idiot has a great point.
I've read about 4 books on OOP recently, well over 1000 pages total of OOP and it just takes time to learn. The examples are always small so they are useless. It's only when you are part of a big project where you begin to see the scope and power of OO design... I wish I started off with another language like Java so that the transition to OOP would be a little easier.. |
You are need reading true books, the classical manuals: Martin Fowler, Gradi Buch and other. It's fundamental authors.
I think, the main problem in OOP-style - it's intersection the relation model data and object model. (See ORM http://en.wikipedia.org/wiki/Object-relational_mapping) |
Quote:
PHP Code:
PHP Code:
|
hello world in patterns OOP-style: http://www.phppatterns.com/docs/desi...ld_in_patterns
|
Correct. The fundamentals are very important. I read this one book that was on fundamentals and concepts of OO design so it had nothing to do with PHP.
I just need to get more experience with it on a daily basis...sometimes you have to put the books down and just get to it! |
Quote:
Right. I guess I worded it wrong. Short codes like this help you see the STRUCTURE of OO so you can begin to understand how OO functions. I guess what I was trying to say is that it's hard to see the power and scope of OO with short simple scripts... Like you said, all that work just to print Hello World can be done in .0002 seconds with regular procedural code so anyone learning OO would have to understand that you wouldn't use OO for the sake of printing JUST Hello World... |
| All times are GMT. The time now is 11:54 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0