Classes in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Classes in PHP

    In this article, I'll explain how to use classes in PHP 5. So, let's start!

    Every class definition begins with the keyword "class", followed by a class name, followed by a pair of curly braces which enclose the definitions of the class' properties and methods.

    The class name can be any valid label which is a not a PHP reserved word (like public, private, addslashes, etc.). A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

    A class may contain its own constants, variables (properties), and functions (methods).
    PHP Code:
    <?php
    class myClass {
        public 
    $myVar "myVar";
        
        public function 
    myFunction() {
            echo 
    $this->myVar;
        }
    }
    ?>
    The keyword $this is available in all classes. When you use the "get_class($this)", you will get the name of the class name:
    PHP Code:
    <?php
    class myClass {    
        function 
    getClassName() {
            echo 
    get_class($this);
        }
    }
    ?>
    The output of this will be: myClass

    Well, let's say we include the file class.myclass.php (see below) in index.php (below too), where we want to use the class' methods and properties:

    File: class.myclass.php
    PHP Code:
    <?php
    class myClass {
        public 
    $myVar "myVar";
        
        public function 
    myFunction() {
            echo 
    $this->myVar;
        }
        
        public function 
    getmyVar() {
            return 
    $this->myVar;
        }
    }
    ?>
    File: index.php
    PHP Code:
    <?php
    include('class.myclass.php');
    $instance = new myClass();
    echo 
    $instance->myVar "<br>";
    $instance->myFunction() . "<br>";
    echo 
    $instance->getmyVar();
    ?>
    The output of this will be:
    myVar
    myVar
    myVar

    Let's take a closer look at the code:
    First, we include the class file. If we wouldn't do that, PHP gives an error that "myClass" is not defined.
    Then, we create a new instance of the class "myClass". If you don't set a new instance and just use myClass::getmyVar(), the script won't work correctly because "$this" is not defined.
    With the just created instance of "myClass", we can use the property "myVar" and the methods "myFunction()" and "getmyVar()". The difference between the methods is that "myFunction()" echoes the variable "myVar" immediately and "getmyVar()" just returns "myVar".


    What about inheriting a class?

    That goes with the keyword "extends". Let's take a look at the following example:
    PHP Code:
    <?php
    include('class.myclass.php');

    class 
    childClass extends myClass {    
        public function 
    echoMyVar() {
            echo 
    parent::myVar;
            
    parent::myFunction();
            echo 
    parent::getmyVar();
        }
    }

    $instance = new childClass();
    $instance->echoMyVar();
    ?>
    This does exactly the same as the example with "index.php", but now the class "childClass" extends "myClass", what means it inherits it's methods, properties, etc. However, if the function "getmyVar" in class "myClass" was declared with the keyword "private" instead of "public", the script won't work. Private functions are not visible for an inherited class.

    You can also define "constants" in PHP classes. Constants always remain unchangeable Here's an example:

    PHP Code:
    <?php
    class constExample {
        const 
    constVar "constant value";
        
        public function 
    echoConst() {
            echo 
    $this->constVar;
        }
    }

    $inst = new constExample();
    $inst->echoConst();
    ?>
    Well, let's see how constructors/destructors work in the classes:

    PHP Code:
    <?php
    class baseClass {
       function 
    __construct() {
           echo 
    "Class constructed";
       }
      
       function 
    __destruct() {
           echo 
    "Class destructed";
       }
    }

    $inst = new baseClass();
    ?>
    This will echo "Class constructed", because you defined a new class. When the class gets destructed (when exit() or something is called), the script will output "Class destructed". Of course, you can pass parameters with __construct and __destruct, as they work as normal methods.


    Well, this are the very basics of classes in PHP. I hope you have learned something about this!
    2
    Yes
    100.00%
    2
    No
    0.00%
    0
    I don't know how to do is.
    0.00%
    0
    BakGat
    Code:
    class Counter {
    public:
      void Count();
      int  ReadDisplay();
    private:
      int  CurrentCount;
    };








    Back up my hard drive? How do I put it in reverse?
    My Community
    BakGat
    sigpic
Working...
X