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 make a component in laravel 10

How to create component in Laravel 10 ?

March 23, 2023March 16, 2024

In this tutorial i will show you simple html based component in Laravel 10 using blade. Creating reusable components is a fundamental aspect of modern web development, as it allows developers to simplify code and improve efficiency. Laravel 10, one of the most popular PHP frameworks, comes with a robust…

Read More
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 CRUD with Search, Image and Pagination

Laravel CRUD with Search, Image and Pagination

July 1, 2022November 6, 2023

In this article i will learn laravel CRUD with Search, Image and Pagination in laravel. In this post we will not only cover the CRUD operation but also the validation on form, unique validation, image uploading and view the uploaded image, Flash messages, Search the uploaded data and many more….

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