Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default PHP5 Classes Tutorial.

    As asked for:

    PHP5 Classes

    Public, protected and private.

    Public functions and variables are accessible from within the class, the class’ childs and externally. Protected functions and variables are accessible from within the class and the class’ childs but not externally. Private functions and variables are only accessible from within the class

    Example:

    PHP Code:
    <?php
      
    class testone {
                  public 
    $hiya;
                  protected 
    $secret;
                  private 
    $topsecret;
                  
                  protected function 
    myFunction() {
                              
    //do something
                  
    }
      }
       
      class 
    testtwo extends testone {
                  private 
    $msg;
                  public function 
    getSecret() {
                              
    //Will work as secret is protected, making it accessible from its child
                              
    return $this->secret;
                  }
                  
                  private function 
    getMsg() {
                              return 
    $this->msg;
                  }
       
                  function 
    getTopSecret() {
                              
    //Won't work as topsecret is private.
                              
    return $this->topsecret;
                  }
                  
                  function 
    doSomething() {
                              
    //Will work as myFunction is protected and this class is a child of testone
                              
    return $this->myFunction();
                  }
      }
       
      
    $classone = new testone();
       
      
    //Will work fine
      
    echo $classone->hiya;
       
      
    // Won't work as its protected
      
    echo $classone->secret;
       
      
    // Also won't work as its private
      
    echo $classone->topsecret;
       
      
    $classtwo = new testtwo();
       
      
    //Will work fine. Function is public and since the class is a child of testone it can access secret.
      
    echo $classtwo->getSecret();
       
      
    //Won't work as getMsg is private
      
    echo $classtwo->getMsg();
      
    ?>
    __construct function

    If a __construct function is defined in you class, it is automatically ran and passed the parameters you sent when you create the class.

    __destruct function

    The __destruct function, if defined, is called when the class is destroyed with unset or when it is no-longer used in the script. You can use this to do any clearing up or perhaps run a SQL query on the database to make any changes you need.

    Examples:

    PHP Code:
    <?php
      
    class testone {
                  function 
    __construct($name) {
                              echo 
    "Hello " $name "!";
                  }
                  
                  function 
    __destruct() {
                              echo 
    "Bye, bye!!";
                  }
      }
       
      
    $classone = new testone("Tom");
      echo 
    "lalala";
       
      
    /*
      Outputs:
      Hello Tom!
      lalala
      Bye, bye!!
      */
      
    ?>
    Static Functions and Variables

    Static functions and variables are functions or variables that when inherited by a child class can be overwritten but also can still be access in their original form. Furthermore static classes are also accessible without creating a new instance of the class that they are a member of. They are accessible by using parent::functionName (When called from a child class) or classname:functionName where classname is the name of the class and functionName is the name of the static function.

    Example:

    PHP Code:
    <?php
      
    class testone {
                  static function 
    hiya($name) {
                              return 
    "Hiya " $name "!!!";
                  }
      }
       
      class 
    testtwo extends testone {
                  function 
    sayHi($name) {
                              echo 
    parent::hiya("Tom");
                              
    // Outputs: Hiya Tom!!!
                  
    }
                  
                  function 
    hiya($name) {
                              return 
    "I am mean!!! Go away.";
                  }
                  
                  function 
    sayHiTwo($name) {
                              echo 
    $this->hiya;
                              
    // Outputs: I am mean!!! Go away.
                  
    }
      }
       
      
    ?>
    Final functions and Variables

    Final functions and variables are, as the name suggests, the final version of that function or variable and cannot be overwritten by a child class.

    Example:

    PHP Code:
    <?
      
    class testone {
                  final function 
    hiya($name) {
                              return 
    "HAHAHA. Go away. This time you can't get rid of me!!";
                  }
      }
       
      class 
    testtwo extends testone {
                  
    //Attempting to overwrite the function creates a error and halts the script.
                  
    function hiya($name) {
                              return 
    "Hiya " $name "!!!";
                  }
      }
      
    ?>
    Interfaces

    Interfaces are like templates for classes. While interfaces cannot be used as classes they can be implemented by a child class. If implemented the child class must follow the functions and/or variables outlined by the interface or a fatal error will occur.

    Example:

    PHP Code:
    <?php
      
    interface theinterface {
                  public function 
    Hiya();
                  private function 
    Secret();
      }
       
      class 
    goodclass implements theinterface {
                  public function 
    Hiya() {
                              echo 
    "hiya";
                  }
                  private function 
    Secret() {
                              echo 
    "psst.. secret";
                  }
      }
       
      class 
    badclass implements theinterface {
                  
    //creates a fatal error because badFunction was not declaired in the interface
                  
    public function badFunction() {
                              echo 
    "Muhahaha";
                  }
      }
    This is quite a simple tutorial and only outlines some of the power of classes in PHP5. If you are interested in a bit of reading I recommend PHP 5 Objects, Patterns, and Practice. You can buy it from: http://www.apress.com/book/view/1590593804

  2. #2
    Join Date
    Apr 2007
    Posts
    2,431
    Tokens
    0

    Latest Awards:

    Default

    WTH!!

    You say this is simple!!!

    Hehe, i might start to learn PHP soon...
    +rep - will help PHPers

    [X] [X] [X]

  3. #3
    Join Date
    May 2006
    Posts
    1,797
    Tokens
    0

    Latest Awards:

    Default

    This is quite a simple tutorial
    Id hate to see what you think is complicated..

    But good TUT, i think :S
    Coming and going...
    Highers are getting the better of me

  4. #4
    Join Date
    Oct 2007
    Location
    Noob Town.
    Posts
    91
    Tokens
    0

    Default

    Great TUT. I'll read tomorrow.

    Thanks a lot!

    My first User Bar


    Sign Up at
    http://www.habtown.net/forum/

    THANKIES>




  5. #5
    Join Date
    Oct 2007
    Location
    Luton, England
    Posts
    1,548
    Tokens
    388
    Habbo
    DeejayMachoo

    Latest Awards:

    Default

    This will help loads for people who write in php 4, they will have a big shock when everyone upgrades to 5 if they dont learn now. +rep

  6. #6
    Join Date
    Jan 2007
    Location
    Canada eh?
    Posts
    766
    Tokens
    75

    Default

    Hahaha...

    I do have to agree with most people above that this tutorial isn't exactly "simple" - a basic understanding of PHP Functions and Classes is required before you try and read this tutorial to actually get much out of it - no offense intended.

    And also as a sidenote - just because PHP will "officially" stop supporting PHP 4 soon doesn't necessarily mean that PHP 4 will become obsolete and un-usable, especially seeing as plenty of hosts and servers are still using PHP 4 atm and have no plans to upgrade. Its kinda like the whole IE 6/IE 7 thing, it will take a very long time for EVERYONE to migrate and for you to be able to code in PHP 5 and have it work on ANY server!

    P.S.
    You have no idea how frustrating it is to try and use a script when all of a sudden it starts complaining that it can't do anything because you only have PHP 4 and its built for PHP 5... ya...

  7. #7
    Join Date
    Oct 2007
    Location
    Luton, England
    Posts
    1,548
    Tokens
    388
    Habbo
    DeejayMachoo

    Latest Awards:

    Default

    Quote Originally Posted by Scriptz View Post
    P.S. You have no idea how frustrating it is to try and use a script when all of a sudden it starts complaining that it can't do anything because you only have PHP 4 and its built for PHP 5... ya...
    Run a php 4 script on a php5 server then and deal with that

  8. #8
    Join Date
    Jan 2007
    Location
    Canada eh?
    Posts
    766
    Tokens
    75

    Default

    Quote Originally Posted by Mattx.org View Post
    Run a php 4 script on a php5 server then and deal with that
    Last I checked most pre-PHP 5 scripts would also run on a PHP 5 server - and correct me if I'm wrong but doesn't PHP 5 just add functionality, not remove previous functions?

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

    Latest Awards:

    Default

    Quote Originally Posted by Scriptz View Post
    Last I checked most pre-PHP 5 scripts would also run on a PHP 5 server - and correct me if I'm wrong but doesn't PHP 5 just add functionality, not remove previous functions?
    Your right :]
    Its not like a PHP4 application wouldn't run on a PHP5 server..

  10. #10
    Join Date
    Jan 2007
    Location
    England, Uk, World, Universe,
    Posts
    1,012
    Tokens
    0

    Latest Awards:

    Default

    If a php 4 app wont run on PHP5, thats crappy.
    my sig ran away,

Page 1 of 2 12 LastLast

Posting Permissions

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