search
HomeBackend DevelopmentPHP ProblemWhat does php static mean?

What does php static mean?

Jun 10, 2021 am 09:08 AM
static

php static is a keyword in PHP. Using the static keyword means that the member is a static member. Only one copy will be retained during the loading process of the class. All operations on static variables will All objects work.

What does php static mean?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What does php static mean?

The role and difference of the static keyword in PHP

static in PHP is different from other object-oriented languages ​​such as Java. An instantiated object only Static methods can be accessed, but static members cannot be accessed.

Using the static keyword means that the member is a static member. Only one copy will be retained during the loading process of the class. All operations on static variables will affect all objects.

In Static variables in PHP cannot be called by instantiated objects, static methods can be called by objects

// ----类内部----
// 调用普通成员
this->name;
// 调用静态成员
self::name_static;
// ----类外部----
// 调用普通成员需要实例化使用
Car c = new Car();
c->name;
// 调用静态方法
c::fun()  <==>  Car::fun()
// 调用静态变量
Car::name;

Analyze a piece of code:

class Car
{
    private $name;
    private static $type = "Car";
    function __construct($name)
    {
        $this->name = $name;
        echo "Car " . $name . " has created!\n";
    }
    public static function getType()
    {
        echo self::$type . "\n";
    }
    public function getName()
    {
        echo "Car name is " . $this->name . "\n";
    }
    function __destruct()
    {
        echo "Car " . $this->name . " has destory!";
    }
}

An entity class defines a constructor, a static function, and a Ordinary function, a destructor, an ordinary member variable, and a static member variable.

Use PHPunit for testing

class  test extends PHPUnit_Framework_TestCase
{
    public function test_car()
    {
        $car = new Car("BMW");
        $car::getType();
        $car->getName();
    }
}

You can get the output:

Car BMW has created!
Car
Car name is BMW
Car BMW has destory!

[Recommended learning: PHP video tutorial]

The above is the detailed content of What does php static mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.