Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Static Keyword in object oriented programming in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 135
    Comment on it

    Static variables and methods is very useful feature in Php oops. We can access static method and variable outside and within class, without creating object of these class. Often, we hear about static class, but there are no class is static, actually when all methods and properties is static within a class then it will be called as a static but here are not any static keyword for class. Static variable and method in Php oops will be treated as public if no-access modifiers is defined.

     

    We use Scope resolution operator (::) to access the property of static variable or method. Static properties of any class is a property that is  directly accessible from class. We can declare static property using static keyword.


    Here is the basic example of static variable and static method.

    <?php
    class Hello
    {
    	public static $first; //Static variable
    	public static function helloWorld()
    	{
    		echo "This is the static funvtion of Hello Class";
    	}
    }
    
    Hello::$first = 5;
    echo Hello::$first;
    
    echo "<br/>";
    
    Hello::helloWorld();
    
    ?>
    

    Above simple example states declaration of the static keyword.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: