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

What is the difference between self and this in php

王林
王林Original
2020-06-30 09:06:395148browse

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");

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

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!

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