何时以及为什么我应该在类中使用public,private和protected函数和变量?他们之间有什么区别?
例子:
// Public
public $variable;
public function doSomething() {
// ...
}
// Private
private $variable;
private function doSomething() {
// ...
}
// Protected
protected $variable;
protected function doSomething() {
// ...
}
Run Code Online (Sandbox Code Playgroud) 遇到 PHP 错误
严重性:8192
消息:不推荐创建动态属性 CI_URI::$config
文件名:core/URI.php
线路编号:102
回溯:
文件:C:\xampp\htdocs\inv_perpus\index.php 行:288 函数:require_once
是否可以测试PHP中的变量是否是静态的?我正在尝试创建一个__get也看静态变量的魔术方法.我发现property_exists()当变量也是静态时返回true.但我需要使用::而不是->我期望的?
我\xe2\x80\x99m 在 PHP 8.2\xe2\x80\x99s 动态属性弃用方面遇到了困难。I\xe2\x80\x99m 在这里寻找与 PHP 9.0 兼容的永久解决方案,因此添加属性#[AllowDynamicProperties]是对我来说不是一个解决方案。
这里\xe2\x80\x99是我的问题。例如,我曾经有以下课程:
\nclass planet {\n function __construct($id, $a, $m, $i, $e, $\xcf\x80) {\n $this->a = $a;\n $this->e = $e;\n $this->i = $i;\n $this->id = $id;\n $this->m = $m;\n $this->n = .9856076686 / ($a * sqrt($a));\n $this->\xcf\x80 = $\xcf\x80;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n然后我将使用以下数组创建每个行星:
\n$pl = [\n new planet("Mercury", .38709831, 3.3011e23, 7.004986, .20563, 77.456119),\n new planet("Venus", .723332982, 4.8675e24, 3.394662, .006772, 131.563703)\n];\nRun Code Online (Sandbox Code Playgroud)\n等等。
\n如果我现在尝试这个,我当然会收到现在著名的通知:\nDeprecated: Creation of …