当前位置:网站首页>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】486-预测赢家
- [development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)
- Bing.com網站
- /bin/ld: 找不到 -llz4
- 6092. Replace elements in the array
- 【LeetCode】1020-飞地的数量
- /bin/ld: 找不到 -lxml2
- Basic knowledge of cryptography
- 党史纪实主题公益数字文创产品正式上线
- College entrance examination admission score line climbing
猜你喜欢

【LeetCode】1162-地图分析

Party History Documentary theme public welfare digital cultural and creative products officially launched

Bing.com网站

树-二叉搜索树

彻底弄懂浏览器强缓存和协商缓存

Yolo format data set processing (XML to txt)
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis

怎样从微信返回的json字符串中截取某个key的值?

LeetCode刷题——验证二叉树的前序序列化#331#Medium

Engineer evaluation | rk3568 development board hands-on test
随机推荐
2303. 计算应缴税款总额
数组和链表的区别浅析
高考录取分数线爬取
已知两种遍历序列构造二叉树
[leetcode] 977 square of ordered array
提前批院校名称
【LeetCode】977-有序数组的平方
/bin/ld: 找不到 -lxml2
【LeetCode】1140-石子游戏II
Bing. Com website
(4) Flink's table API and SQL table schema
6095. Strong password checker II
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)
floyed「建议收藏」
4. Jctree related knowledge learning
MySQL calculate n-day retention rate
【Salesforce】如何确认你的Salesforce版本?
[leetcode] 417 - Pacific Atlantic current problem
03. Preliminary use of golang