i

PHP Tutorial

PHP-Static Methods and Properties

In Object Oriented programming used in PHP, static methods are the methods that can be called directly without creating any object for that class. You can declare these methods using static keyword.

Syntax:

class class_name {

public static function function_name ()

{

//code to be executed

}

}

You can use the static method using (::) and method name.

For example:

class Hello {

public static function greeting() {

echo “Welcome to our website!”;

}

}

Hello :: greeting(); //Calling static method without creating object.

?>

Similarly, we can call static properties declared using static keyword without creating an instance of a class.

Syntax:

class class_name {

public static $variable_name = value;

}

?>

You can use the static property using double colon and property name.

For example:

class max {

public static $max_visitors = 40;

}

echo max :: $max_visitors ;

?>

In above example we have declared a static property and a value of the variable. And we have called it without creating any object of the max class.