Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2014
    Location
    United Kingdom
    Posts
    7
    Tokens
    60
    Habbo
    joshua06860

    Default [Tutorial][PHP] An introduction to OOP.

    Hey guys,


    Just here to give a quick intro into PHP OOP (Object-Oriented Programming).
    Now, a way I like to look at this is by using a person as an example.
    Every person is a human being. So, if we were using PHP to create humans (though, I would hope we weren't created by PHP) I may have a class called Human - which may look something like this:-

    PHP Code:
    <?php        namespace GodOnly;        class Human{                    }    ?>
    Now, inside the Human class I will probably have a few different functions. One for creating all the necessary things the human needs to live (heart etc...).
    So let's define a few of those.


    PHP Code:
    <?php namespace GodOnly;        class Human{                        function createHair(){ /* an awesome god-like function to create hair */ }                        function createEyes(){ /* an awesome god-like function to create eyes */ }                        function createBrain(){ /* an awesome god-like function to create brains */ }                        /* etc... */        }    ?>
    Okay! Great! I've got my class. How do I use it?
    Well, first of all you need to create an instance of that class and assign it to a variable.
    Um, what? Just do as I do below (in this example I include my class into the file in which I want to use those functions).

    PHP Code:
    <?php        require_once("awesome_god_like_human_maker.php");        /* some more god-like code created by a god-like person (me) */                $human_maker = new \GodOnly\Human(); //Create a new instance of our class        $human_maker->createBrain(); //Initiate our awesome brain-making function!    ?>
    Now, if we're going to be creating thousands of humans with different attributes we're not going to want to go through this code over and over again just
    to change a few things like eye color, hair color and intellect. Well, it's a bloody good job we don't have to because we can pass information to those functions just as we would in procedural.


    PHP Code:
    <?php        require_once("awesome_god_like_human_maker.php");        /* some more god-like code created by a god-like person (me) */                $human_maker = new \GodOnly\Human();        $human_maker->createBrain(98); //Here I've passed along an expected IQ score to that brain.    ?>
    Changes to "awesome_god_like_human_maker.php":
    function createBrain($iq_score){ /* $iq_score will equal whatever we pass to it when we call that function */ }

  2. #2
    Join Date
    Jul 2008
    Location
    Dublin, Ireland.
    Posts
    13,083
    Tokens
    2,964
    Habbo
    Yet

    Latest Awards:

    Default

    Instructions were not clear enough, i got my **** stuck in the toaster.

    moderator alert Edited by e5 (Forum Super Moderator) - Please do not post pointessly.
    Last edited by e5; 10-09-2014 at 05:27 AM.

  3. #3
    Join Date
    May 2007
    Posts
    10,481
    Tokens
    3,140

    Latest Awards:

    Default

    Interesting tutorial, it's a little hard to follow and I suspect only those who already understand OOP will understand the tutorial. A Very large critique from me is that without the use of line-breaks the code is completely incomprehensible. Cool none the less.
    Chippiewill.


  4. #4
    Join Date
    Sep 2014
    Location
    United Kingdom
    Posts
    7
    Tokens
    60
    Habbo
    joshua06860

    Default

    Sorry about that, the PHP tags seem to have broken my line breaks and tabs.

  5. #5
    Join Date
    Jun 2008
    Location
    United Kingdom
    Posts
    2,015
    Tokens
    568

    Latest Awards:

    Default

    I agree with Chippiewill, I was going to say the same things.
    It needs a bit more work, perhaps a proper introduction, then the tutorial, then a small conclusion just to give it some structure. It doesn't really explain what OOP is and why it's used and whatnot, but I like the way it's written and this forum could do with some more tutorials and other things to help beginners, so I like it

  6. #6
    Join Date
    Jul 2005
    Location
    North Wales
    Posts
    4,233
    Tokens
    2,009

    Latest Awards:

    Default

    I think its great that your contributing content to the community but as people have said the structure of the formatting kind of knocks it off a little. Having said that I've gotta give it to you that it's probably the simplest way I've seen OOP with PHP explained a lot of people seem to make out that its hard to understand.

Posting Permissions

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