Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Static keyword

What is static keyword and properties in a php/java class?

Aman Jain, June 13, 2021September 21, 2021

Static keyword are isolated, which means we can access the property of a class without creating a object/instance of class. static methods that are common to all the objects of the class. Hence, any logic which can be shared among multiple instances of a class should be inside the static functions. let’s have a quick example:

<?php
class NameOfClass {
  public static function myStaticMethod() {
    echo "Hello World!";
  }
}
?>

we can access a static method class name double colon(::) scope resolution operator, and the method name:

NameOfClass::myStaticMethod();

We can not access static method by creating an object

$classobject=new NameOfClass();
$classobject->myStaticMethod();

it will produce error, static member can not access as non-static member.

Let’s go more deep dive in

<?php
/* Use of static method in PHP */
  
class NormalA {
      
    public function test($var = "Hello World") {
        $this->var = $var;
        return $this->var;
    }
}
  
class NormalB {
    public static function test($var) {
        $var = "This is static";
        return $var;
    }
}
  
// Creating Object of class A
$obj = new NormalA();
  
echo $obj->test('This is non-static'); 
echo "\n";
echo NormalB::test('This is non-static'); 
  
?>

Output

This is non-static
This is static

Self keyword is used to access only static methods, we can’t access not static method using self. you can use $this to access the non static methods in a class

<?php 
class SelfClassName{

  public static function testself(){

  }
  public function test(){
     self::testself();  // we can access static function and properties in non static methods
     SelfClassName::testself(); //correct
  }
  public function test2(){
     $this->testself(); // wrong access, we can't access static properties as statically 
  } 
}

class childClass extends SelfClassName{

   public static function testself(){

   }

   public function test3(){
    parent::testself();
    self::testself(); 
   }

}

I hope it will clear all doubts.

Related

Php Softwares

Post navigation

Previous post
Next post

Related Posts

Php How to Get Only Soft Deleted Records in Laravel 9

How to Get Only Soft Deleted Records in Laravel 9 ?

June 20, 2022June 20, 2022

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records…

Read More
Php Laravel group by with example

How to use laravel group by with example ?

January 22, 2022January 22, 2022

MySQL group by is used to group same column value multiple rows into one row so to create the group by query we used groupBy method. laravel groupBy method accepts single and multiple parameter as column name. In this article i will show you to use groupBy in multiple ways,…

Read More

How to handle TokenMismatchException Ajax in Laravel 8 ?

December 30, 2021December 30, 2021

Laravel by default protects the application from unauthorized commands executed from outside the application means a suspicious user wanted to perform form submission from external command. But Laravel by default creates a token for every post request which we need to verify before reaching to out application logic. TokenMismatchException is…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version