Home > Backend Development > PHP Problem > What is the difference between self and this in php

What is the difference between self and this in php

王林
Release: 2023-03-01 18:06:02
Original
5216 people have browsed it

The difference between self and this in php is: this is a pointer to the current object instance, it does not point to any other object or class; self points to the class itself, which means that self does not point to anything that has been instantiated Object, self is generally used to point to static variables in the class.

What is the difference between self and this in php

Difference analysis:

this

this is determined at the time of instantiation. who. Therefore, this is a pointer to the current object instance and does not point to any other object or class.

Example:

$this->name =$name;
print( $this->name)
$obj1 = new name("PBPHome");
Copy after login

self

self points to the class itself, that is, self does not point to any instantiated object. Generally, self is used to point to static variables in the class.

If we use static (usually keyword static) members in the class, we must also use self to call. Also note that using self to call static variables must use :: (field operator symbol).

Example:

$this->lastCount =++self::$firstCount
Copy after login

If you want to know more related knowledge, please visit php Chinese website.

The above is the detailed content of What is the difference between self and this in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template