Searching...
Saturday 13 October 2012

Constant Members And Static Members In PHP

10:42 pm

Constant Members And Static Members In PHP

Constant Members And Static Members In PHP :

Constant member datas:
A member data of a class whose value be changed or that cannot assigned is called as a constant member data.
1.      A constant member data should not be prefered with ‘$’ sign and it should not be explicitely access specified.
2.      A constant member data should be declared by using the keyword const.
3.      A constant member data connot be invoked by an instance of a class, it can be invloked by the class name with scope resolution operator(::).
Static members:
1.      Declasing class member data or methods as “static” makes them accessible without an instance /object of the class.
2.      A member data declared as static in a class cannot be accessed with an instantiated class object where as a static method can be invoked.
3.      The static methods are callable without an instance of the class created so that the pseudo variable $this is not available inside the method declared as static.
4.      Static members are declared by using the keyword static.
       
<?php

class myclass
{
    const name="india tech";
    public static $age = 25;
    static function myfun()
    {
        echo "inside static function<br>";
    }
}
?>
<h1>
    <?php
    $obj = new myclass();
    echo $obj->name."<br>";
    echo myclass::name."<br>";
    echo $obj->age."<br>";
    echo myclass::$age."<br>";
    $obj->myfun();
    myclass::myfun();
    ?>
</h1>
OutPut:
india tech
25
inside static function
inside static function

0 comments:

Post a Comment