PHP STATIC METHODS

Sugar babies

PHP STATIC METHODS

  • Static methods can be called directly - without creating an instance of the class first.
  • Static methods are declared with the static keyword.
  • The static keyword is used to declare properties and methods of a class as static. 
  • Static properties and methods can be used without creating an instance of the class.
  • The static keyword is also used to declare variables in a function which keep their value after the function has ended.

EXAMPLES OF STATIC METHODS :

  • Weight of a Body.
  • Car Resting on a Bridge
  • Pushing a Heavy Block.
  • A Portrait Hung on the Wall.
  • Ship Floating on Water Surface.
  • An Object Placed on a High Surface.
  •  A Person Standing on the Ground.

SYNTAX FOR STATIC METHODS IN PHP :

Access_modifier static void methodName() 
{
 // Method body. 

EXAMPLE FOR STATIC METHODS IN PHP :

<?php
class  greeting 
{
    public static function  
welcome ( ) 
      {
         echo " Hello Rosa!";
       }
}

//  call static  methods
greeting:: welcome ( );
?>

OUTPUT :

Hello Rosa!
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send