Usage of static and const keywords in php

墨辰丷
Release: 2023-03-28 11:42:02
Original
1449 people have browsed it

This article mainly introduces the usage of static and const keywords in php, and analyzes the functions, usage and related precautions of static and const keywords in the form of examples. Friends in need can refer to it

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:

##

<?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>";
?>
Copy after login

The output result is:

中国
我的祖国是:中国
中国-江苏
Copy after login

Static methods in a class can only access 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();
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php code optimization

php Multiple inheritance Several methods

php-fpm.conf configuration instructions

The above is the detailed content of Usage of static and const keywords 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!