DEV Community

Cover image for PHP Static Methods & Properties
BazengDev
BazengDev

Posted on • Edited on

2 1

PHP Static Methods & Properties

Static Methods

Static methods can be called directly - without creating an instance of the class first.
Static methods are declared with the static keyword:

<?php
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }
}
// Call static method
greeting::welcome();
?>
Enter fullscreen mode Exit fullscreen mode

A static method can be accessed from a method in the same class using the self keyword and double colon (::):

<?php
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }

  public function __construct() {
    self::welcome();
  }
}

new greeting();
?>
Enter fullscreen mode Exit fullscreen mode

Static methods can also be called from methods in other classes. To do this, the static method should be public:

<?php
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }
}

class SomeOtherClass {
  public function message() {
    greeting::welcome();
  }
}
?>
Enter fullscreen mode Exit fullscreen mode

To call a static method from a child class, use the parent keyword inside the child class. Here, the static method can be public or protected.

<?php
class Name{
  protected static function getName() {
    return "Bazeng";
  }
}

class SayName extends Name {
  public $name;
  public function __construct() {
    $this->name= parent::getName();
  }
}

$name= new SayName;
echo $name ->name;
?>
Enter fullscreen mode Exit fullscreen mode
Static properties

Static properties are declared with the static keyword they can be called directly - without creating an instance of a class.Example:

<?php
class Person{
   public static $name = "Bazeng";
 }
echo Person::$name;
Enter fullscreen mode Exit fullscreen mode

To access a static property in a method in same class use self

<?php
class Person{
   public static $name = "Bazeng";
   public function sayName(){
     echo self::$name;
   }
 }

 $person = new Person();
 $person->sayName();

Enter fullscreen mode Exit fullscreen mode

To access a static property in a method in a parent class use parent

<?php
class Person{
   public static $name = "Bazeng";

 }
class SayName extends Person{
   public function __construct(){
    echo parent::$name;
   }
} 

$person = new SayName();
Enter fullscreen mode Exit fullscreen mode

Image of Bright Data

Global Data Access Unlocked – Reach data across borders without restrictions.

Unlock the power of global data collection with our advanced proxy solutions. Ideal for market research and more.

Unlock Data Now

Top comments (0)

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay