Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    65
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Getting my head around OOP

    I've been learning PHP for the past few weeks. All was going quite well until the introduction of Classes, Objects and all that goes with them. I had the same problem when i was learning C++ many years ago. I've got some kind of mental block when it comes to this area.

    Can anyone prescribe some useful reading for me? Even better would be if someone could describe to me in layman's terms the idea behind classes and objects, the correct terminology, examples of where they are used and why?

    I'm currently reading "Learning PHP, MySQL and Javascript" and looking at phpmanual.net when i run into something awkward; but i've reached a stumbling block here. Would appreciate some help.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Being an old-time programmer from a COBOL-centric mindset, I too have struggled with OO, first understanding the _how_, and then with the _why_. I'm still inclined to ignore most of OO when I program which I admit is a weakness.

    This book was good:

    http://amzn.to/jyD7WM

    I read another book which catered to a COBOL background which is my favorite OO conecpts book, but it's out of print apparently. Another sign the dinosaurs are retiring!

    If it makes you feel any better, you can code PHP completely and satisfactorily and ignore the OO side. In fact, OO was kind of shoe-horned into PHP, so it works fine just using a function-based technique. Much more important than using OO is to separate the presentation layer from the function layer. PHP can get really messy really fast if you imbed everying right within your HTML markup. Ew.

    Another suggestion I have: Install, learn, and use CodeIgniter to build a simple website. It's a PHP framework that uses classes/objects. For me it was helpful because I was able to see and work within a practical application of OO.

    The LEAST helpful OO teaching tool (and I bristle every time I see this) is to use some abstract impractical example to explain how OO works. Class "dog"? Attribute "fur"? class "Horse" extends class "dog"? WTF people, give me an example that I can apply to a real-life programming situation.
    Last edited by Fumigator; 05-23-2011 at 09:47 PM.

  • Users who have thanked Fumigator for this post:

    gnolan (05-23-2011)

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    65
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Being an old-time programmer from a COBOL-centric mindset, I too have struggled with OO, first understanding the _how_, and then with the _why_. I'm still inclined to ignore most of OO when I program which I admit is a weakness.

    This book was good:

    http://amzn.to/jyD7WM

    I read another book which catered to a COBOL background which is my favorite OO conecpts book, but it's out of print apparently. Another sign the dinosaurs are retiring!

    If it makes you feel any better, you can code PHP completely and satisfactorily and ignore the OO side. In fact, OO was kind of shoe-horned into PHP, so it works fine just using a function-based technique. Much more important than using OO is to separate the presentation layer from the function layer. PHP can get really messy really fast if you imbed everying right within your HTML markup. Ew.

    Another suggestion I have: Install, learn, and use CodeIgniter to build a simple website. It's a PHP framework that uses classes/objects. For me it was helpful because I was able to see and work within a practical application of OO.

    The LEAST helpful OO teaching tool (and I bristle every time I see this) is to use some abstract impractical example to explain how OO works. Class "dog"? Attribute "fur"? class "Horse" extends class "dog"? WTF people, give me an example that I can apply to a real-life programming situation.
    It's good to know that i'm not the only one who finds it a bit of a struggle. I think i'm really missing something basic about it, that if someone could point out to me i can't help but feel that everything would click.

    To be honest it's not something that i'd be content to do without. On the contrary it's something that i'd like to master.

    I'll take a look at the book you mentioned and maybe some other aimed specifically at OOP.

    I've come across some of those examples you mentioned above, the likes of: Class Bear extends Class polarBear - PHP for zoologists.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I think a bigger part of the problem when searching for a reasonable example is knowing exactly what it is you are looking for. Generic tutorials will always have arbitrary examples (though horse extends dog is really the stupidest thing I've ever heard since it doesn't make any sense whatsoever lol). PHP4's OOP was a hack in, thats for sure. The 5.x engine though is far more robust, and minus an entry point PHP could actually be viewed as a fully object oriented language (I'll never see it that way, and *may* consider it when an entry point is provided :P).
    Shapes I find to be the norm:
    PHP Code:
    interface IShape
    {
        public function 
    getArea();
        public function 
    getPermiter();
        public function 
    getColour();

    Coming from procedural languages as well created a stumbling block when I first learned OO - especially in PHP. PHP is in fact one of the worst languages to learn OOP imho. Its primary problem is its complete lack of explicit datatyping, and without this datatyping there is a lot of reasoning lost when attempting to describe PHP in an OO fashion. This example is completely useless:
    PHP Code:
    class Car
    {
        public 
    $colour;
        public 
    $make;
        public 
    $model;
        ...

    Public in PHP = very bad. Always. Unlike C++, C#, java etc, that lack of datatype now means I can put whatever I want into a class which completely defeats the point; PHP must be provided with accessors and mutators for any member you want to expose. From members to method returns and arguments, no datatyping is an extreme problem.

    OOP is something that seems to just 'click' and then you can program without any problem in it. Its the shift from the whole data in > process > data out into the paradigm of Object change that is the tricky part.

    I'd go back to C++ or look into C#/Java to learn OOP. These are much tighter languages, so mastering these will make PHP OO a breeze. PHP's OO follows closely to the C#/Java syntax and overall usage, so I'd suggest one of these two. Just go in knowing that interfaces are King, and you will come out laughing (C# and java do not allow multiple inheritance like C++ does). That and treat every class as a datatype, and away you go.

  • Users who have thanked Fou-Lu for this post:

    gnolan (05-24-2011)

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Fumigator is correct when he says that you can get by without OOP in PHP but I was the other way round when I got my head around the concept of OOP which is similar in all programming languages. I now tend to use OOP wherever practicable.

    A class (or object) is a standalone piece of code that contains the properties and methods (call them functions if you prefer) to manipulate those properties.

    Hopefully these "KISS" demos might help you understand the basics of a class.

    The demos create a php class to create rectangles. The class has properties to store the length and width of each rectangle and methods to set and retrieve the length and width and calculate the area of each rectangle.

    The first demo shows how to create a rectangle class and then test drive it by changing lengths and widths and outputing the length, width and areas. The demo creates 2 rectangles using the single class.

    The second demo shows how you can then use the rectangle class to dynamically create rectangular divs in the html and style them according to the properties of each created rectangle and then output the area of each div in the html.

    If you view the source html of demo 2 in your browser, you will see the css and html code that is created by the php code.

    demo 1

    PHP Code:
    <?php

    class rectangle {

        protected 
    $length;
        protected 
    $width;
        protected 
    $fillColor;

        public function 
    __construct($length$width$fillColor) {
            
    $this->length $length;
            
    $this->width $width;
            
    $this->fillColor $fillColor;
        }

        
    // Accessor methods/functions
        
    public function getLength() {
            return 
    $this->length;
        }

        public function 
    getWidth() {
            return 
    $this->width;
        }

        public function 
    getFillColor() {
            return 
    $this->fillColor;
        }

        
    //Mutator methods/functions
        
    public function setLength($length) {
            
    $this->length $length;
        }

        public function 
    setWidth($width) {
            
    $this->width $width;
        }

        public function 
    setFillColor($fillColor) {
            
    $this->fillColor $fillColor;
        }

        public function 
    getArea() {
            return 
    $this->length $this->width;
        }

    }

    //end of class
    // code to test drive the class
    $rect1 = new rectangle(100200'red');
    $rect2 = new rectangle(7080'green');
    //now display the area of each rect
    echo 'area of rect 1 = ' $rect1->getArea() . '<br />';
    echo 
    'area of rect 2 = ' $rect2->getArea() . '<br />';
    //now change the length of each of the 2 rectangles
    $rect1->setLength(50);
    $rect2->setLength(80);
    //now display the area of each rect
    echo '<br />area of rect 1 with new length = ' $rect1->getArea() . '<br />';
    echo 
    'area of rect 2 with new length = ' $rect2->getArea() . '<br />';
    ?>
    demo 2

    PHP Code:
    <?php

    class rectangle {

        protected 
    $length;
        protected 
    $width;
        protected 
    $fillColor;

        public function 
    __construct($length$width$fillColor) {
            
    $this->length $length;
            
    $this->width $width;
            
    $this->fillColor $fillColor;
        }

        
    // Accessor methods/functions
        
    public function getLength() {
            return 
    $this->length;
        }

        public function 
    getWidth() {
            return 
    $this->width;
        }

        public function 
    getFillColor() {
            return 
    $this->fillColor;
        }

        
    //Mutator methods/functions
        
    public function setLength($length) {
            
    $this->length $length;
        }

        public function 
    setWidth($width) {
            
    $this->width $width;
        }

        public function 
    setFillColor($fillColor) {
            
    $this->fillColor $fillColor;
        }

        public function 
    getArea() {
            return 
    $this->length $this->width;
        }

    }

    //create an array of rectangle objects
    $rectangles = array();
    $rectangles[] = new rectangle(100200'red');
    $rectangles[] = new rectangle(7080'green');
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css">
    <?php
    $count 
    1;
    foreach (
    $rectangles as $rect) {
        echo 
    '#div' $count++ . '{';
        echo 
    'width: ' $rect->getWidth() . 'px;';
        echo 
    'height: ' $rect->getLength() . 'px;';
        echo 
    'background-color: '.$rect->getFillColor().';';
        echo 
    'border: 1px solid black;';
        echo 
    '}';
    }
    ?>
            </style>
        </head>
        <body>
            <!-- create divs withe the properties of each rectangle -->
            <?php
            $count 
    1;
            foreach (
    $rectangles as $rect) {
                echo 
    '<div id="div' $count++ . '">Area = ' $rect->getArea() . '</div>';
            }
            
    ?>
        </body>
    </html>
    Last edited by bullant; 05-24-2011 at 01:18 AM.

  • Users who have thanked bullant for this post:

    gnolan (05-24-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •