当前位置:网站首页>PHP static members
PHP static members
2022-07-02 15:42:00 【Ximen eats snow】
Definition and access of static members ?
Scene one Class 
Class 
Optimized code
<?php
namespace _1008;
/** * Just imagine , The following two scenarios : * 1. If an object , We only use it once , Is it still necessary to create objects , Directly treat the class as an object , Wouldn't it be more convenient ? * 2. If there are multiple objects , You need to share some properties and methods , And they must be called by independent objects , Can't share , What do I do ? */
class Demo1
{
// Instance attributes : You need to access the object obtained after instantiating the class
public $product;
// Static members : attribute , Class properties , Static attribute
public static $price;
// Construction method
public function __construct($product, $price)
{
// References to the current object : $this
$this->product = $product;
// Reference to the current class : self
self::$price = $price;
}
// In the static method , Can I call instance members ?
public static function test()
{
// return self::$price;
// return $this->product;
}
}
$obj = new Demo1(' The computer ', 4890);
// :: Range resolver , Class external , Use the class name to access static members
echo ' Product Name : '. $obj->product, ', Price : ', Demo1::$price;
//echo '<br>', Demo1::test();
echo '<br>', $obj->test();
Static member call constant
<?php
namespace _1008;
// Class constant
class Demo2
{
// Class constant
const NATION = ' China ';
// Class properties
public static $sex;
public function __construct($sex)
{
self::$sex = $sex;
}
public function get()
{
return self::NATION;
}
}
echo Demo2::NATION;
echo '<hr>';
echo Demo2::$sex;
echo '<hr>';
echo (new Demo2(' male '))->get();
边栏推荐
- 【LeetCode】876-链表的中间结点
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- LeetCode刷题——两整数之和#371#Medium
- Bing.com網站
- Leetcode question brushing - parity linked list 328 medium
- 6096. 咒语和药水的成功对数
- List of sergeant schools
- Yolo format data set processing (XML to txt)
- 2278. 字母在字符串中的百分比
- 【LeetCode】189-轮转数组
猜你喜欢

Soul torture, what is AQS???

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData

全是精华的模电专题复习资料:基本放大电路知识点
![[experience cloud] how to get the metadata of experience cloud in vscode](/img/45/012c2265402ba1b44f4497f468bc61.png)
[experience cloud] how to get the metadata of experience cloud in vscode

自定义异常

Deux séquences ergodiques connues pour construire des arbres binaires

4. Jctree related knowledge learning
![[leetcode] 417 - Pacific Atlantic current problem](/img/30/c541bc1e81eb4e348ca11116a05e84.png)
[leetcode] 417 - Pacific Atlantic current problem

【LeetCode】1162-地图分析

PTA ladder game exercise set l2-001 inter city emergency rescue
随机推荐
List of sergeant schools
制作p12证书[通俗易懂]
/bin/ld: 找不到 -lpam
[leetcode] 19 delete the penultimate node of the linked list
beforeEach
Leetcode skimming -- sum of two integers 371 medium
Engineer evaluation | rk3568 development board hands-on test
[leetcode] 1020 number of enclaves
6096. Success logarithm of spells and potions
夏季高考文化成绩一分一段表
Name of institution approved in advance
【LeetCode】200-岛屿数量
【Leetcode】167-两数之和II -输入有序数组
6092. Replace elements in the array
党史纪实主题公益数字文创产品正式上线
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Custom exception
04. Some thoughts on enterprise application construction after entering cloud native
[leetcode] 283 move zero