


Static properties contain data to be encapsulated in a class and can be shared by all instances of the class. In fact, in addition to belonging to a fixed class and restricting access methods, the static properties of a class are very similar to the global variables of a function.
We use a static property Counter::$count in the following example. It belongs to the Counter class and not to any instance of Counter. You cannot use this to refer to it, but you can use self or other valid naming expressions. In the example, the getCount method returns self::$count, not Counter::$count.
Static methods implement functions that need to be encapsulated by the class and have nothing to do with specific objects. Static methods are very similar to global functions. Static methods can fully access the attributes of the class or be accessed by instances of the object, regardless of whether the access qualifier is What.
In the previous example, getCount is an ordinary method, called with ->. PHP creates a this variable, although the method is not used. However, getCount does not belong to any object. In some cases, we even If you want to call it when there is no valid object, then you should use static methods. PHP will not create this variable inside static methods, even if you call them from an object.
Example 6.7 comes from 6.3 changing getCount to a static method. The Static keyword cannot prevent an instance from using the -> operator to call getCount, but PHP will not create this variable inside the method. If you use this-> to call, an error will occur.
//Example 6.3 refers to Section 4 --Examples in constructors and destructors (see the previous article). By comparing the two examples, you can have a good grasp of the difference between
//static methods and ordinary methods.
You can write a method by judging this Whether established shows whether it is called statically or non-statically. Of course, if you use the static keyword, this method will always be static no matter how it is called.
Your class can also define constant properties, no need To use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class.
Listing 6.7 Static members
Copy code The code is as follows:
class Counter
{
private static $count = 0;
const VERSION = 2.0;
function __construct()
{
self::$count++;
}
function__destruct()
{
self: :$count--;
}
static function getCount()
{
return self::$count;
}
};
//Create an instance, then __construct() will execute
$c = new Counter( );
//Output 1
print(Counter::getCount() . "n");
//Output the version attribute of the class
print("Version used: " . Counter::VERSION . "n");
?>
The above introduces the static members of the object model, PHP 50 object model in-depth exploration, including the content of the object model. I hope it will be helpful to friends who are interested in PHP tutorials.

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
