Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use self in php

Shulou Source: shulou.com Published: 2022-06-01 13:19:58 11月23日 Update

This article mainly introduces the relevant knowledge of how to use self in php, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use self in php. Let's take a look at it.

In PHP, self means "oneself, itself"; the self keyword is used to point to the class itself and to the class instance, and the self keyword is not to point to any object that has been instantiated, but to point to static variables in the class.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

What does self mean in php

The use of self in php:

Self points to the class itself, that is, the PHP self keyword does not point to any objects that have been instantiated, and self is generally used to point to static variables in the class.

1. Self can access static properties and static methods in this class, and can access static properties and static methods in the parent class.

2. When using self, you don't have to instantiate it.

< ?phpclass Counter{undefined//定义属性,包括一个静态变量private static $firstCount = 0;private $lastCount;//构造函数function __construct(){undefined$this->

LastCount = selft::$firstCount;// uses the PHP self keyword to call static variables, and self calls must use: (field operation symbol)} / / print the maximum value function printLastCount () {undefinedprint ($this- > lastCount);}} / / instantiate the object $countObject = new Counter (); $countObject- > printLastCount (); / / output 1? >

We only need to pay attention to two places here, line 6 and line 12.

We defined a static variable $firstCount on the second line, with an initial value of 0, so we called this value on line 12, using self to call, and using "::" to connect.

Is what we call the domain operator, so what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it just has to do with the class.

So I call the class itself, so we can't use this to reference, we can use self to reference.

Because self points to the class itself, it has nothing to do with any object instance. In other words, if there are static members in our class, we must also use self to call.

This is the end of the article on "how to use self in php". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use self in php". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Static instance point variable object key keyword knowledge content that is attribute meaning method article operation no two that is value just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Redmi Docker Shulou Tech Info