当前位置:网站首页>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();
边栏推荐
- 数组和链表的区别浅析
- 全是精华的模电专题复习资料:基本放大电路知识点
- Force deduction solution summary 2029 stone game IX
- Thoroughly understand browser strong cache and negotiation cache
- 6090. Minimax games
- [leetcode] 200 number of islands
- [experience cloud] how to get the metadata of experience cloud in vscode
- matlab中wavedec2,说说wavedec2函数[通俗易懂]
- 2279. 装满石头的背包的最大数量
- [leetcode] 1254 - count the number of closed Islands
猜你喜欢

PTA 天梯赛习题集 L2-001 城市间紧急救援
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis

Thoroughly understand browser strong cache and negotiation cache

Loss function and positive and negative sample allocation: Yolo series

4. Jctree related knowledge learning

Bing. Com website

Leetcode skimming -- incremental ternary subsequence 334 medium

Bing. Site Internet

LeetCode刷题——递增的三元子序列#334#Medium

03. Preliminary use of golang
随机推荐
怎样从微信返回的json字符串中截取某个key的值?
Redux——详解
(5) Flink's table API and SQL update mode and Kafka connector case
[leetcode] 977 square of ordered array
folium,确诊和密接轨迹上图
LeetCode刷题——递增的三元子序列#334#Medium
NBA player analysis
Yolo format data set processing (XML to txt)
(4) Flink's table API and SQL table schema
6090. Minimax games
How to intercept the value of a key from the JSON string returned by wechat?
C # get PLC information (kepserver) II
Folium, diagnosis and close contact trajectory above
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
【idea】推荐一个idea翻译插件:Translation「建议收藏」
密码学基础知识
高考录取分数线爬取
MySQL calculate n-day retention rate
【LeetCode】189-轮转数组
[salesforce] how to confirm your salesforce version?