I have been dealing with PHP codings for a over two years, but i have yet to use object oriented structure in any of my programming. What is the benefit of using object oriented method to design something. When should i use it? Why cant we do it with ordinary variables within a function? Please suggest. Thanks. Ps. If you're unsure of what im asking, its those codes with $this->blabla()
When you said "those codes with $this->blabla() I automatically think php classes. Classes can take some time to understand and write for some, but in the end it saves you tons of time and many lines of codes. Read, study and test the code in this article...it will open your eyes to the powers http://www.php-editors.com/articles/simple_php_classes.php
Here is an Introductory PHP Oops Tutorial http://devzone.zend.com/article/638-PHP-101-part-7-The-Bear-Necessities Oops using languages http://www.google.com/Top/Computers/Programming/Languages/Object-Oriented/
...Because it makes your code a lot nicer as it has much more protocol to it than the modular approach.
I also NEVER use classes. I use functions and everything else, but never classes. I need to start using them more as well
I just wrote an article about that very question, if you're interested it's at hxxp://www.smarterreviews.com/blog/php-oop-mysql-how-to-use-classes-in-a-web-application/ (Software won't let me do a real link yet)
If your writing simple 1 or 2 page applications, then OO is probally a bit of over kill. However if you are writing full application, say an online mail client, which can span lots of pages and thousands of lines of code, then OO is very useful. I use it a lot in Java, and the best way I can explain it is that it lets me break a large complex problem into manageable chunks. Where each class will represent a particular function area (e.g Screen Display), or data representation (e.g. email message).
Having worked on large scale projects with frameworks made entirely in OOP and writing just as large projects in PP I find that the majority of the hype about OOP is more a personal preference. While it can be time saving in some cases it can also make a project that you didn't do from start to finish a nightmare. It does have it uses, but theres almost nothing you can do in PHP OOP than can't be done in some way or another using procedural programming.
I think using functions is easier because you don't have to declare an object every time you need to use a function (method).
one thing i never understood is, if functions use more resources than the a arguments themselves then why does everyone get so object happy when it it isn't needed. just to do it?
Ah, and that must make all procedural programming "fake" then, right? Yeah, C was/is never used for "real" programming. And by complexity I assume you are trying to say the capability (programmers don't want complex code), in which case you are incorrect. You can use procedural methods to accomplish anything that you can do in OO and vice versa. It's just like the argument of which language is the best. Really, most languages can do what the other languages can do, it's just that some are better for a given task. OO is a way of approaching programming. You use objects as blueprints and containers. You don't have to create an object every time you need to use a function/method as some people seem to be suggesting. You could have a date object and do $date::showCurrentTime(); You can also use magic methods that allow you to dynamically adapt your object to your needs (e.g., "showCurrentTime()" might not even be defined, but you could have your class know that "showXXX()" means "echo getXXX()" to let you quickly display data). Object-Oriented programming keeps your code cleaner, especially on large projects, and helps with reusability. The goal is to create simple objects that do fairly specific things. They can be extended for more complex tasks. For simple scripts, you don't need to use OOP (nor do you really need to use it for longer ones), and because of that many PHP programmers know little about OOP. Personally, I'd recommend learning about it and making use of it. You'll often find that code becomes more logical (and legible). The more that you use it, the more you realize how nearly everything falls into an object quite logically. If you standardize the way you work with PHP, you can organized your classes and have PHP auto-include necessary ones (like symfony does).... Anyway, starting to get off topic, but it's really just a way of approaching coding. Even if you love procedural coding, I recommend trying OO as well. You will learn a lot
Most Mainframe programmer must have a smile at the corner of their mouth now! Now more seriously. I am also programming in PHP and frankly the benefit in using classes seems not to be so neat as in some other languages. It is obvious that using OO coding will raise the re usability of your code, but a function's list would bring the same kind of benefits in most cases. Some people are doing everything using classes, some other never use. I do believe that each implementation should be evaluated considering the project you are working into. If you work alone in a simple project, making just dynamic pages, then using OO programming would just make your code more complicated obliging you to comply to OO reality that standard programming just ignores. Now, if you work in a larger project, with several programmers in which you have identified some individual parts, that some other programmers doing linked tasks can use already as finished-small-bricks, then you will get a lot of benefits using such a method, giving you the impression that you making a construction game. Nowaday, when i choose to use OO, i ask myself the following: will i reuse that piece of code in another implementation? if yes, then i try to use classes.
I do not think that PHP lends itself well to OO. To me it is the most confusing implantation of object orientation than any other language. I don't even bother with it much.