簡體   English   中英

從 class 外部訪問 class 屬性

[英]Access class property from outside of class

假設我有以下 class:

class MyClass {
    public function Talk() {
        $Say = "Something";
        return $Say;
    }
}

然后我啟動了一個 class 的實例:

$Inst = new MyClass();

我現在如何在 MyClass 之外調用 $Say,例如,在文檔上回顯它? 例如,有類似的東西:

$Said = "He" . $Say

我強烈建議您通讀http://php.net/manual/en/language.oop5.php 它將教您PHP中的OOP的基礎知識。


在您的示例中, $Say只是在Talk()的 scope 中聲明的另一個變量。 不是class 屬性。

使其成為 class 屬性:

class MyClass {
    public $say = 'Something';

    public function Talk() {
        return $this->say;
    }
}

$inst = new MyClass();
$said = 'He ' . $inst->say;

然而,這違背了Talk()的目的。
最后一行應該是$said = 'He '. $inst->Talk(); $said = 'He '. $inst->Talk();

$say不是 class 屬性。 如果是這樣,您將像這樣定義您的 class:

class MyClass {
    public $say;      
}

相反,它是 function Talk()的局部變量。 如果您想以定義 class 的方式訪問它,您可以:

$instance = new MyClass();
$instance->Talk();

您需要使 $Say 成為 MyClass class 的即時變量。

class MyClass {
    public $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
}

然后,您可以通過 $Inst->Say 從 class 外部訪問實例變量

此外,更好的做法是封裝您的 class 實例變量並使用“getter”方法來檢索值。

class MyClass {
    private $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
    public getSay() {
        return $this->Say;
    }
}

$Inst = new MyClass();
echo $Inst->getSay();

您可以使用

$said = "He" . $Inst->Talk();

在這種情況下,或者您可以 class

class MyClass {
var $say;
public function Talk() {
    $say = "Something";
    $this->say = $say;
    return $say;
}
}

並打電話

$said = "He" . $Inst->say;

您需要在函數之前聲明該 var,例如

class MyClass {
  public $say;
  function Talk() {
    $this->say = "something";
  }
}

接着

$Said = "He ".$Inst->$say;

oop 的最佳實踐是永遠不要在您的 class 中擁有公共屬性。 操作 class 屬性的最佳方法是使用單獨的方法來返回屬性值並設置屬性值。 所以

class MyClass {
    private $say;

    // common setter, invoked when we trying to set properties as they are public
    public function __set($name, $value) {
        $methodname = 'set' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            $this->$methodname($value);
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // common getter, invoked when we trying to get properties as they are public
    public function __get($name) {
        $methodname = 'get' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            return $this->$methodname();
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // setter for out private property $say, invoked by common setter when colling $a->say = "something";
    public function setSay($value) {
        $this->say = $value;
    }

    // getter for out private property $say, invoked by common getter when colling $a->say;
    public function getSay() {
        return $this->say;
    }

    public function Talk($monologue) {
        $this->say = (!empty($monologue))?$this->say:$monologue;
        return $this->say;
    }

}

因此,現在您可以訪問您的私有屬性,因為它們是公開的,並進行所有必要的檢查,以免將錯誤值存儲在其中。 像那樣:

$a = new MyClass;
$a->say = "Hello my friends !";
$a->Talk();
$a->Talk("Good bye !");
echo $a->say;

或者像這樣:

$a = new MyClass;
$a->setSay("Hello my friends !");
$a->Talk();
$a->Talk("Good bye !");
echo $a->getSay();

為了提高安全性,您可以將 setSay 和 getSay 方法設為私有,但第二段代碼將不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM