OOPS in PHP 5 Tutorial - Static Keyword
This article will guide you on the static keyword used in PHP5. Unlike the methods and data members used in OOPS where the scope is decided by access specifiers, the static methods/attributes are available as a part of the class. So it is available to all the instance defined for the class. To implement static keyword functionality to the attributes or the methods will have to be prefix with “static” keyword.
To assign values to static variables you need to use scope resolution operator(::) along with the class name.
Example:
< ? class ClassName { static private $staticvariable; //Defining Static Variable function __construct($value) { if($value != "") { ClassName::$staticvariable = $value; //Accessing Static Variable } $this->getStaticData(); } public function getStaticData() { echo ClassName::$staticvariable; //Accessing Static Variable } } $a = new staticmethod("12"); $a = new staticmethod("23"); $a = new staticmethod(""); ?>
Output:
12
23
23
Explanation:
- Here i have declared static variable $staticvariable
- In the constructor i am checking and value and then assigning the value to the static variable
- Finally the getStaticData() method will output the static variable content
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Good article…