PHP STATIC PROPERTIES

Sugar babies

PHP STATIC PROPERTIES

  • Static properties can be called directly - without creating an instance of a class.
  • Static properties are declared with the static keyword.
  • Static properties and methods can be used without creating an instance of the class.
  • Static properties are used when we'd like to store class-level data, also not bound to an instance.
  •  The static keyword is also used to declare variables in a function which keep their value after the function has ended.
  • Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ).

SYNTAX FOR STATIC PROPERTIES IN PHP :

<? php 
class testclass
{
 static $name="test";
 } 
echo testclass::$name; 
$var="testclass"; 
echo $var::$name;
?>

 EXAMPLE FOR STATIC PROPERTIES IN PHP :

<?php
class pi 
 {
    public static $value = 3.14159;
}
// Get static property
echo pi : : $value;
?>

OUTPUT :

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

GocourseAI

close
send