gpt4 book ai didi

PHP:类与实例变量

转载 作者:行者123 更新时间:2023-12-03 18:52:02 27 4
gpt4 key购买 nike

我知道在 Java 和 C# 等编程语言中类变量和实例变量存在差异,所以我想知道 PHP 是否相同。

所以我知道类变量在该类的所有实例之间共享,而实例变量仅与该类的特定实例相关。

例如:

class db {
private $host; <--
private $user; <-- These will be treated as instance variables
private $pass; <-- as they are set by the class constructor
private $dbname; <--

private $connected = false; <-- Will this be treated as a class
variable? Shared among all the
instance of the db class?

public function __construct($host, $user, $pass, $dbname) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;
}

public function checkConn() {
// some code here to change the value of $this->connected
}

最佳答案

PHP 有 static class properties .您的代码中没有任何属性被声明为静态的,因此它们都是实例属性。

关于PHP:类与实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37413323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com