当前位置:网站首页>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】1020-飞地的数量
- [leetcode] 877 stone game
- 自定义异常
- Pytoch saves tensor to Mat file
- Solve the problem of frequent interruption of mobaxterm remote connection
- [leetcode] 189 rotation array
- Redux - detailed explanation
- Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
- Bing.com網站
- [leetcode] 344 reverse string
猜你喜欢

Build your own semantic segmentation platform deeplabv3+

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

SQL transaction

Yolo format data set processing (XML to txt)

Custom exception

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

Leetcode question brushing - parity linked list 328 medium

已知兩種遍曆序列構造二叉樹

【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
随机推荐
[leetcode] 189 rotation array
彻底弄懂浏览器强缓存和协商缓存
密码学基础知识
解决BASE64Encoder报错的问题
高考分数线爬取
PostgresSQL 流复制 主备切换 主库无读写宕机场景
/bin/ld: 找不到 -llz4
士官类学校名录
【Salesforce】如何确认你的Salesforce版本?
Soul torture, what is AQS???
lseek 出错
Name of institution approved in advance
【LeetCode】200-岛屿数量
[leetcode] 200 number of islands
制作p12证书[通俗易懂]
6096. Success logarithm of spells and potions
【Leetcode】167-两数之和II -输入有序数组
Evaluation of embedded rz/g2l processor core board and development board of Feiling
[leetcode] 877 stone game
Two traversal sequences are known to construct binary trees