Home  >  Article  >  Backend Development  >  Analysis on usage of static and const keywords in php

Analysis on usage of static and const keywords in php

高洛峰
高洛峰Original
2017-01-06 14:04:221133browse

The examples in this article describe the usage of static and const keywords in php. Share it with everyone for your reference, the details are as follows:

The member properties and member functions described by the static keyword in the class are all static.

Static members can restrict external access, because static members belong to the class, not to any object instance.

From a memory perspective, the object is placed in "heap memory", the reference to the object is placed in "stack memory", and the static members are placed in the initialization static segment. Added when loading. Can be shared by all objects in memory. As shown in the figure below:

Analysis on usage of static and const keywords in php

<?php
class Person{
 public static $myCountry = "中国";
 public static function say(){
  echo "我的祖国是:".self::$myCountry."<br>";
 }
}
//输出静态属性
echo Person::$myCountry."<br>";
//调用静态方法
Person::say();
//修改静态属性
Person::$myCountry = "中国-江苏";
echo Person::$myCountry."<br>";
?>

The output result is:

中国
我的祖国是:中国
中国-江苏

The static method in the class can only access the static properties of the class. Static methods in a class cannot access non-static members of the class. We use self to access static properties in a class. self is similar to this, except that self represents the class where the static method is located. This is similar, except that self represents the class where the static method is located. This refers to the pointer, which represents the object that calls this method. Static methods are not called with objects, so there is no this reference. There is no reference to this. Without this, there is no way to call other member properties in the class.

const is a keyword that defines constants. Const is often used to define constants in classes. The access method of member attributes modified with "const" is similar to the access method of members modified with "static". They also use the "class name" and the "self" keyword in the method. But you don't need to use the "$" symbol, and you can't use objects to access it.

<?php
class MyClass{
 const constant = &#39;constant value&#39;;
 function showConstant(){
  //方法中调用常量,没有$
  echo self::constant."<br>";
 }
}
//类直接调用,没有$
echo MyClass::constant."<br>";
$class = new MyClass();
$class ->showConstant();
?>

I hope this article will be helpful to everyone in PHP programming.

For more articles related to analysis of the usage of static and const keywords in PHP, please pay attention to 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