Results 1 to 8 of 8
  1. #1
    Join Date
    May 2007
    Location
    Nebo, NC, USA
    Posts
    2,517
    Tokens
    0

    Latest Awards:

    Default [PHP-Tutorial] Introduction to classes.

    Well this is a tutorial on the introduction to PHP classes.


    Required knowledge:

    Basic knowledge of PHP.
    Functions.


    Lets begin, you know to tell apache to enter PHP mode we need to enter the PHP syntax:

    PHP Code:
    <?php

    ?>
    Lets start to build our class, we are going to make a "check username" class.

    First we need to declare that this is going to be a class:

    PHP Code:
    <?php

    class check_username
    {

    }

    ?>
    class - tells PHP this is a class
    check_username is the name of the class used later in this tutorial.

    Lets begin with a VAR. It is a variable, just like $bob is a variable.

    PHP Code:
    <?php

    class check_username
    {
        var 
    $username;
    }

    ?>
    Now, lets begin our "check username" function:

    PHP Code:
    <?php

    class check_username
    {
        var 
    $username;

        function 
    check()
        {

        }
    }

    ?>
    That declares that we are ready for a function. Lets add a little to the function to check the username. We need a username to check against so lets add another variable.

    PHP Code:
    <?php

    class check_username
    {
        var 
    $username;
        [
    B]var $c_username;[/B]
        
        function 
    check()
        {

        }
    }

    ?>
    Now lets make it check the username:

    PHP Code:
    <?php

    class check_username
    {
        var 
    $username;
        var 
    $c_username;

        function 
    check()
        {
            if (
    $this->username != $this->c_username)
            {
                echo 
    "That username is not correct!";
            }
            else
            {
                echo 
    "That is a correct username!";
            }
        }
    }

    ?>
    if($this->username != $this->c_username)
    if the variable $username does not equal the variable $c_username then show "That username is not correct!" otherwise show "That is a correct username!"

    $this->username brings the variable $username
    $this->c_username brings the variable $c_username.

    Now save that as username_class.php

    Here is how to use it:

    PHP Code:
    <?php

    include "username_class.php"// Includes class.

    $username = new check_username// Starts the class, check_username is the name we started earlier.
    $username->c_username "Caleb"// Username to check against.
    $username->username "Bob"// Username to check.

    ?>
    Comments should help.

    Thanks,
    Caleb!

  2. #2
    Join Date
    Nov 2006
    Location
    Cheshire.
    Posts
    730
    Tokens
    250

    Default

    That's very good, I may start learning too use classes soon
    EDIT: Fancy getting back to me on that problem Denta?
    Last edited by Frog!; 26-10-2007 at 11:54 PM.


    Give us an add like!

  3. #3
    Join Date
    May 2007
    Location
    Nebo, NC, USA
    Posts
    2,517
    Tokens
    0

    Latest Awards:

    Default

    Classes are just... awesome

  4. #4
    Join Date
    May 2007
    Posts
    467
    Tokens
    0

    Default

    hmm i maybe wrong but by looking at it, it would show a blank page you would have do to some thing like this

    PHP Code:
     <?php

    include "username_class.php"// Includes class.

    $username = new check_username// Starts the class, check_username is the name we started earlier.
    $username->c_username "Caleb"// Username to check against.
    $username->username "Bob"// Username to check.
    $username->check() // show the data.

    ?>
    i maybe wrong but yes....

  5. #5
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    To be honest, its really no use learing how to use PHP4-style classes now. As at the end of this year PHP4 will become offically unsupported by PHP. Its best to learn how to use PHP5-style classes. I might write a tutorial on it if people are intrested.

  6. #6
    Join Date
    May 2007
    Location
    Nebo, NC, USA
    Posts
    2,517
    Tokens
    0

    Latest Awards:

    Default

    $username->check();

    Do it tom! :]

  7. #7
    Join Date
    Aug 2006
    Location
    Manchester, UK
    Posts
    2,016
    Tokens
    141
    Habbo
    florx

    Latest Awards:

    Default

    Quote Originally Posted by Tomm View Post
    To be honest, its really no use learing how to use PHP4-style classes now. As at the end of this year PHP4 will become offically unsupported by PHP. Its best to learn how to use PHP5-style classes. I might write a tutorial on it if people are intrested.
    Yeah go on - im interested

  8. #8
    Join Date
    Jul 2005
    Location
    -
    Posts
    2,995
    Tokens
    0

    Latest Awards:

    Default

    Thanks caleb Bookmarked.

Posting Permissions

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