Home Backend Development PHP Tutorial Understanding the static keyword in php

Understanding the static keyword in php

Nov 26, 2019 pm 05:35 PM
php static

Understanding the static keyword in php

Understanding of the static keyword in php

Understanding of static static variables

The static variable type specifier is static.

Static variables belong to the static storage method, and their storage space is the static data area in the memory (storage units are allocated in the static storage area). The data in this area occupies these storage spaces throughout the running of the program. (It is not released during the entire running of the program), and it can also be considered that its memory address remains unchanged until the end of the entire program (on the contrary, auto automatic variables, that is, dynamic local variables, belong to the dynamic storage category and occupy dynamic storage space. Functions Released after the call is completed). Although static variables always exist throughout the execution of the program, they cannot be used outside its scope.

In addition, quantities belonging to static storage methods are not necessarily static variables. For example: Although external variables belong to the static storage method, they are not necessarily static variables. They must be defined by static before they can become static external variables, or static global variables.

All global variables are static variables, and local variables are local static variables only when they are defined with the type modifier static.

Static variables can be applied anywhere. Once the application is successful, it will no longer accept other similar applications.

Static variables do not mean that they cannot change the value. A quantity that cannot change the value is called a constant. The value it holds is mutable, and it will remain up-to-date. It is said to be static because it does not change when the function is called or exits. That is, if we assign a certain value to a static variable the last time the function is called, the value will remain unchanged the next time the function is called.

Static variables within the function

static usage

1, please see the following example:

function doStuff(&$cache) {
    static $cache = null;
    if ($cache === null) {
        echo $cache = '%heavy database stuff or something%';
    }
}
$cache = 'not null';
doStuff($cache);
// Output
%heavy database stuff or something%
Copy after login

And, in doStuff( ) function, the static variable $cache is not immutable, $cache changed from null to %heavy database stuff or something%; from the above example, we can see that the static keyword affects reference transfer, even if we use & To try to change the value and address of the variable $cache, it still does not affect the if judgment in the doStuff() function;

Static methods and attributes in the class

● We Treat the class as a template for generating objects, use the object as an active component, instantiate a class, get an object, and then access the methods and properties of the object.

For example $foo = new Foo(); $foo is the object after instantiation of class Foo.

● Static methods are functions with class as scope. We can directly access static methods without instantiation.

For example:

class Foo()
{
    public static function a(){}
}
// 访问a();
Foo::a();
Copy after login

● In the current class ( To access static methods or attributes in non-subclasses) use self::method(), note: self can call static methods and attributes of the parent class; ● Static methods cannot access ordinary attributes and methods in this class, because those attributes and methods Belonging to an object, static methods and properties are also called methods of class variables.

Delayed static binding

Let’s look at an example first

header("Content-type: text/html; charset=utf-8");
class A
{
    public static function aa()
    {
        echo "非延迟静态绑定<br>";
    }
     
    public static function bb()
    {
        echo self::aa();  // Output 非延迟静态绑定
        echo static::aa(); // Output 延迟静态绑定
    }
}
class B extends A
{
    public static function aa()
    {
        echo "延迟静态绑定";
    }
     
    public static function cc()
    {
        echo self::bb();
    }
}
B::bb();
// Output
非延迟静态绑定
延迟静态绑定
Copy after login

After php5.3, we can use static to get the aa() of the subclass Method, which refers to the class being called. Using the self keyword refers to the current class (A), so what is obtained is the return value of the aa() method of class A;

Summary:

Lazy binding of the static keyword It has many uses and can generally be summarized while working on a project.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Understanding the static keyword in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles