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 use left join in Laravel Eloquent

How to use left join in Laravel eloquent query with example

February 5, 2022February 8, 2024

In this article we will learn to use left join in laravel eloquent and query builder. Laravel itself provide inbuilt method to left join the table like in MySQL or SQL based database we can do join multiple tables using leftJoin function. For example if you have 10 rows and…

Read More
Php How to access and setup storage files after upload in laravel

How to access and setup storage files after upload in laravel ?

May 23, 2022May 26, 2022

In this article i will show you to access and setup uploaded storage files in laravel. This is strongly recommended that not to use direct URL from storage, laravel internally handle it by creating Symblink but some developers access the files of storage directly without knowing the consequences of website…

Read More
Php Extract/Unzip a Zip File in Laravel

How to Extract/Unzip a Zip File in Laravel ?

May 22, 2022November 5, 2023

In laravel Extract/Unzip zip a file can be achieved by ZipArchive library,its gives easy to use flexibilities to developers so they can easily Extract/Unzip a Zip File in Laravel and integrate the Zip creation activity in the project. Zip files are used to create lossless data compression and store multiple…

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

  • 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

  • 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
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version