Hello Digitalpointians; I've been [slowly] learning how to use OOP for use in PHP; and just one question: how is it useful? Also: how did you learn to code in the OOP style? Thanks; - Marshton
Think of it like this. A program/application is a big pile of packing peanuts. With procedural programming, you always have a big pile of packing peanuts. With OOP, you have a few bags with the packing peanuts in them. Much easier to move around. Much easier to work on individual parts. And much easier to give to someone else to work with. With OOP, just start slow and simple. Make something like a shopping cart class, or a database class. Then more to OOP driven applications, where most everything is accessed via an object. Finally move to MVC or another framework.
I find it extremely useful for making things modular and making it easy to reuse code. Over the years I have developed an object library that lets me abstract away all the tedious, repetitive parts of site development (forms, tables, logins, loading/saving from database, internationalisation, etc.) and instead focus on the unique functionality of a particular site. This makes me much more productive; in some cases I can create quite elaborate sites without doing much work at all. If I had to do all that with procedural code it would be an unholy mess and far more difficult to reuse.
Good advice from previous posters. You will quickly realize the benefits once you start get the hang of it, and you will. I'm sure it won't take long I suggest that you besides from reading the resources on the internet also buy yourself a book (or a few) and just learn by doing, in your own pace.
Try to learn different PHP OOP frameworks to get a hang of it.I recommend CodeIgniter ,very easy to learn and great documentation.
Thanks guys: I'll try using CodeIgniter Out of interest, anyone have any examples of OOP scripts that I can look at? Thanks!
wing. Awesome! +rep for you! edit: Can you guys tell me if this code for Connecting to a Database is OK or have I missed some, strict OOP rule/s? <?php class db { public function __construct($dbname,$dbuser,$dbpass,$host = 'localhost') { $this->dbname = $dbname; $this->dbuser = $dbuser; $this->dbpass = $dbpass; $this->host = $host; } public function debug() { echo mysql_error(); } public function connect() { if (@mysql_connect($this->host,$this->dbuser,$this->dbpass) == FALSE) { $this->debug(); return FALSE; } if (@mysql_select_db($this->dbname) == FALSE) { $this->debug(); return FALSE; } return TRUE; } } $db = new db('test','root',''); $db->connect(); PHP: Thanks Edit 2: Yes. Root w/ no password is an example. :|
For someone beginning to learn PHP, should they try to learn OOP right from the start or should they ignore OOP until they have first got a grasp of PHP in general? Im asking because im unsure if learning PHP in general, then trying to get an understanding of OOP is like trying to learn the old way first, only to not use it and dismiss it, to then take on the task of learning OOP. Or is it still necessary to get to grips with PHP anyway? Even if the understanding of OOP is the ultimate goal for them. BTW, i do understand that OOP is not another programming language, im not confusing PHP and OOP as if they were two separate langages.
I dont put any code outside of the class, a class should have its own file. you should also add the __deconstruct function to close the database, and anything else that should be cleaned up at the end.