Quick Guide To PHP Static Properties

The purpose of this article is to introduce the concept of PHP static properties, including how you could declare them and use them in your code.

We will also discuss some useful use cases for static properties, as well as some best practices.

PHP static property is a property shared by all instances of the class.

A static property, in contrast to a regular property, belongs to the class itself, and does not belong to any specific instance of the class. It means that any changes that are made to any static property of one object will have an effect on all the other objects that share the same property.



PHP Static Properties

PHP static properties are properties that belong to a class in general and are not specific to any instance of that class in particular.

PHP static properties are class-level variables that are shared by all instances of a class.

They are declared using the static keyword and can be accessed using the scope resolution operator :: .

Static properties are defined within the class definition, but outside of any methods. They can be initialized with a default value or left uninitialized.

Syntax

<?php
class ClassName {
public static $staticProp = "mrexamples";
}
?>

Syntax

ClassName::staticProp;

As an example, let’s take a look at the following:

Example: 

<?php class Website { public static $name = "mrexamples.com"; } // Get static property echo Website::$name; ?>

Example Explanation

Here, we declare a static property: $name.

Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).

We define a class called a Website with a single static property called $name.

The $name property is defined as “mrexamples.com“.

To access a static property, we use the scope resolution operator (::) followed by the name of the class and the name of the static property. In this case, we access the $name property of the Website class using the following syntax:

Website::$name

The echo statement then outputs the value of the $name property, which is “mrexamples.com”.

There are static methods as well as non-static methods that can be found in a class.

By using the self keyword, as well as the double colon (::), you can gain access to a static property from another method in the same class:

Example: 

<?php class Website { public static $name = "mrexamples.com"; public function printingStaticStr() { return self::$name; } } $name = new Website(); echo $name->printingStaticStr(); ?>
A static property can be called from a child class by using the parent keyword within the child class:

Example: 

<?php class Website { public static $name = "mrexamples.com <br>"; } class Printing extends Website { public function printingStaticStr() { return parent::$name; } }echo Printing::$name; $printing = new Printing(); echo $printing->printingStaticStr(); ?>

Example Explanation

Above code defines a class Website with a static property $name set to the value “mrexamples.com” which contains an HTML line break tag. It also defines a child class Printing that extends Website.

The Printing class has a method printingStaticStr() which returns the value of the $name property using the parent::$name syntax. This method can be used to access the static property of the parent class Website.

In the next section, the code prints the value of the $name property of the Printing class using Printing::$name. This will output “mrexamples.com” since the $name property is inherited from the parent class Website.

Finally, the code creates an object $printing of the Printing class using $printing = new Printing(). This line of code creates a new object of the Printing class.

The code then calls the printingStaticStr() method on the $printing object using $printing->printingStaticStr(). This method returns the value of the $name property of the parent class Website using the parent::$name syntax. Since the value of the $name property contains an HTML line break tag, the output will contain a line break between “mrexamples.com” and any subsequent output.

PHP static properties can also be used to store data that is shared across multiple instances of a class, or to keep track of global state within an application.

However, it is important to use them carefully and avoid overusing them, as they can lead to unexpected behavior if not managed properly.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *