
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Static Properties and Methods
Introduction
In a PHP class, properties and methods declared with static keyword cannot be accessed by its object with the help of -> operator. In fact, object is not required to access any instance of class. Default visibility of static items in a class is public
static properties
To access a static property from outside class, we need to use scope resolution operator (::) along with name of class. A string variable evaluating to name of class can also provide to static property
<?php class testclass{ static $name="test"; } echo testclass::$name; $var="testclass"; echo $var::$name; ?>
to use static propery inside any method of the same class, use self keyword instead of -> operator that is used for accessing instance properties.
<?php class testclass{ static $name="test"; public function test(){ echo self::$name; } } $obj=new testclass(); $obj->test(); ?>
Any static property declared in parent class can be referred to inside a child class method, by using parent keyword along with scope resolution operator
<?php class testclass{ static $name="test"; public function test(){ echo self::$name; } } class myclass extends testclass{ public function mytest(){ echo parent::$name; } } $obj=new myclass(); $obj->mytest(); ?>
static methods
When a method is declared as static, the pseudo-variable $this is not available to it. Hence, instance attributes of a class cannot be accessed in it. The static method is called by name of class along with scope resolution operator
In following example, the class has a static property $count that increnments every time constructor is executed (i.e. for each object). Inside the class, there is a static function that retrieves value of static property
Example
<?php class testclass{ static $count=0; function __construct(){ self::$count++; } static function showcount(){ echo "count = " . self::$count; } } $a=new testclass(); $b=new testclass(); $c=new testclass(); testclass::showcount(); ?>
Output
This will produce following output −
count = 3
- Related Articles
- Passing static methods as arguments in PHP
- Kotlin static methods and variables
- Static Properties in JavaScript
- Static methods vs Instance methods in Java
- Demonstrate static variables, methods and blocks in Java
- Static methods in JavaScript classes?
- How do I create static class data and static class methods in Python?
- What is the difference between non-static methods and abstract methods in Java?
- Explain STATIC AND INSTANCE method in PHP.
- PHP Class properties
- Are static methods inherited in Java?
- Restrictions applied to Java static methods
- Java 8 static methods in interfaces
- PHP Late Static Bindings
- PHP Magic Methods
