2016-05-24 7 views
2

JavaやC#のようなプログラミング言語でクラス変数とインスタンス変数に違いがあることは知っていますので、PHPが同じかどうか疑問に思っていました。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 
    } 

答えて

3

PHPは、static class propertiesを有しています。コード内のどのプロパティもstaticとして宣言されていないので、すべてインスタンスプロパティです。

+0

正しい方向に私を指摘してくれてありがとう[this](http://php.net/manual/en/language.variables.scope.php#59676)は、私が探していたものです。 – J2D8T

関連する問題