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 Filter Data Using Relational Model in Laravel

How to Filter Data Using Relational Model in Laravel ?

June 29, 2022July 1, 2022

In this article i will show you to filter data using the relational model in laravel. Using the laravel relationship between the models we can easily fetch relative data but sometimes we want to fetch the data by filtering the child model that affect the result of parent model. You…

Read More
Laravel Create database connection in laravel

How to make database connection in Laravel 8 ?

December 14, 2021February 22, 2024

Laravel comes with first party support for several database drivers. Laravel gives much better flexibility in terms of using the RDBMS based databases. Laravel supports MySQL, PostgreSQL, SQLite and SQL Server by default but if you want to make connection with Mongo or other database then you need to install…

Read More
Php Laravel whereIn query with example

Mastering Laravel’s whereIn Query with Examples

January 18, 2022October 21, 2023

Are you struggling with Laravel’s whereIn query? In this comprehensive guide, you’ll learn how to harness the power of Laravel’s whereIn Eloquent builder for creating dynamic queries, subqueries, and more. Explore the multiple ways to use this versatile method and gain a deep understanding with practical examples. Below examples will…

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