1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Usefullness of OOP with PHP?

Discussion in 'PHP' started by Marshton, Feb 17, 2010.

  1. #1
    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
     
    Marshton, Feb 17, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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.
     
    jestep, Feb 17, 2010 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    SmallPotatoes, Feb 17, 2010 IP
  4. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    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.
     
    wing, Feb 17, 2010 IP
  5. ryantetek

    ryantetek Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try to learn different PHP OOP frameworks to get a hang of it.I recommend CodeIgniter ,very easy to learn and great documentation.
     
    ryantetek, Feb 17, 2010 IP
  6. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks guys: I'll try using CodeIgniter :)

    Out of interest, anyone have any examples of OOP scripts that I can look at? Thanks!
     
    Marshton, Feb 18, 2010 IP
  7. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Here is a really good beginners php oop guide, lots of examples to look at.
    :)
     
    wing, Feb 18, 2010 IP
  8. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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. :|
     
    Last edited: Feb 18, 2010
    Marshton, Feb 18, 2010 IP
  9. pipes

    pipes Prominent Member

    Messages:
    12,766
    Likes Received:
    958
    Best Answers:
    0
    Trophy Points:
    360
    #9
    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.
     
    pipes, May 2, 2010 IP
  10. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #10
    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.
     
    killaklown, May 2, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Learn PHP in general first then move on to OOP.
     
    danx10, May 2, 2010 IP
    pipes likes this.
  12. pipes

    pipes Prominent Member

    Messages:
    12,766
    Likes Received:
    958
    Best Answers:
    0
    Trophy Points:
    360
    #12
    danx10 thanks, at least i know that im learning the correct way around then.
     
    pipes, May 2, 2010 IP
  13. s.ham

    s.ham Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #13
    Instead of wasting your time. Please start learning Zend Framework. It has everything as you wanted.
     
    s.ham, May 2, 2010 IP