- A Comprehensive Introduction to PHP Object-Oriented Programming (OOP)
- Transitioning from Procedural to Object-Oriented Programming in PHP
- Demystifying the Four Principles of Object-Oriented Programming
- Understanding Classes and Instantiation in PHP
- Understanding the $this Keyword in PHP Classes
- Understanding Accessors in Object-Oriented Programming
- Demystifying PHP Static and Self Keywords
- Understanding Constructors and Their Benefits in PHP
- Understanding Magic Methods: Getters and Setters in PHP
- Understanding the PHP __call Magic Method: Handling Non-Existent Functions
- Understanding the __toString Method in PHP
- A Guide to Inheritance in PHP with Examples
- Demystifying Constructors and Inheritance in PHP: A Comprehensive Guide
- Understanding Method Overriding in PHP
- Understanding Abstract Classes in PHP
- Understanding Interfaces in PHP: A Guide with Examples
- PHP Traits: An In-Depth Exploration with Practical Examples
- Mastering Method Chaining in PHP
- Understanding Type Hinting in PHP
- Understanding PHP Namespaces: Organizing Your Code
- Autoloading Classes with PHP and Namespace
- Building a User Login System in PHP with OOP pt.I
- Building a User Login System in PHP with OOP pt.II
- Building a User Login System in PHP with OOP pt.III
Welcome back to another informative session about PHP. In the Post, we’re diving into the fascinating realm of static properties and the self keyword. If you’ve ever been curious about these concepts, you’re in the right place.
Static Properties
First, let’s talk about static properties. When you declare a property as static within a class, it takes on unique characteristics. Unlike regular properties that belong to class instances, static properties belong to the class itself. Think of it like this: if class instances are the products rolling off the assembly line, the class itself is the factory. Changes to a static property affect the entire factory.
class Product {
public static $total = 0;
public function calculateTotal() {
// Accessing a static property
return self::$total;
}
}
Static properties can still be public, private, or protected, but combining public with private or protected with public is not allowed.
Accessing Static Properties
Now, let’s see how to access a static property. You can do this either through an instance of the class or directly using the class name. For instance, if we have a class Product
with a static property total
, we can access it like this:
$product = new Product();
echo $product->total; // This won't work, as $total is static
// To access it properly, use the class name:
echo Product::$total;
This demonstrates that static properties can be accessed both from instances and the class itself. However, it’s best practice to access them using the class name to avoid issues when class names change.
Changing Static Properties
One critical aspect of static properties is that changes are persistent during the script’s execution. While instance properties vary for each object, a static property’s value remains consistent for the entire script. Here’s an example:
$product = new Product();
$product->total = 200;
$anotherProduct = new Product();
echo $anotherProduct->total; // Outputs 200, even for a different instance
// To modify and access the static property:
Product::$total = 500;
echo Product::$total; // Outputs 500
In this example, we set the static property $total
to 200 for the first instance. When we create another instance, it still holds the modified value. This demonstrates how changes to static properties persist throughout the script’s execution.
The Self Keyword
To access static properties within a class, you can use the self
keyword followed by the scope resolution operator ::
. This allows you to refer to the class itself. For example:
class Product {
public static $total = 0;
public function calculateTotal() {
return self::$total;
}
}
Using self
within the class is essential, especially when the class name may change or for clarity in code. It ensures you’re always referring to the class’s context.
Conclusion
Understanding static properties and using the self keyword in PHP can be tremendously beneficial. Static properties persist throughout a script’s execution, providing a way to share and maintain data across all instances of a class. And the self keyword allows you to access these static properties consistently within the class.
With this newfound knowledge, you can write more organized and efficient PHP code. Stay tuned for more PHP insights and tutorials in the future.