假設我有以下類:訪問類屬性
class MyClass {
public function Talk() {
$Say = "Something";
return $Say;
}
}
然後我開始了類的一個實例:
$Inst = new MyClass();
我現在如何才能調用$所以說外面MyClass的例如,在文檔上回顯它?例如,具有類似:
$Said = "He" . $Say
假設我有以下類:訪問類屬性
class MyClass {
public function Talk() {
$Say = "Something";
return $Say;
}
}
然後我開始了類的一個實例:
$Inst = new MyClass();
我現在如何才能調用$所以說外面MyClass的例如,在文檔上回顯它?例如,具有類似:
$Said = "He" . $Say
我強烈建議你通過閱讀http://php.net/manual/en/language.oop5.php。它將教你使用PHP的OOP基礎知識。
在你的例子中,$Say
只是在Talk()
範圍內聲明的另一個變量。它是而不是一類的屬性。
要使它成爲一個類屬性:
class MyClass {
public $say = 'Something';
public function Talk() {
return $this->say;
}
}
$inst = new MyClass();
$said = 'He ' . $inst->say;
然而這違背了Talk()
目的。
最後一行應該是$said = 'He '. $inst->Talk();
$say
不是一個類屬性。如果是,那麼您需要定義你的類是這樣的:
class MyClass {
public $say;
}
相反,它是功能Talk()
的局部變量。如果您要訪問它,你必須定義的類的方式,你會怎麼做:
$instance = new MyClass();
$instance->Talk();
你需要使$說MyClass類的即時變化。
class MyClass {
public $Say
public function Talk() {
$this->Say = "Something";
return $this->Say;
}
}
然後,您可以從類的外部通過 $ Inst-訪問實例變量>說
此外,它是更好的做法來封裝類的實例變量和使用「的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();
更好的方法是使用'__get'和'__set'隱式調用'getSay',這樣其他開發人員就可以方便地調用'$ inst-> Say'而不用擔心調用訪問器或mutator。 – zzzzBov
@zzzzBov偉大的建議! – IOInterrupt
我有一個[代碼塊](http://www.php.net/manual/en/language.oop5.overloading.php#97572)動態訪問器和增變器。它適用於大多數情況,除非您需要通過參考進行訪問。 – zzzzBov
您可以在此情況下,使用
$said = "He" . $Inst->Talk();
,或者您也可以 類
class MyClass {
var $say;
public function Talk() {
$say = "Something";
$this->say = $say;
return $say;
}
}
,並呼籲
$said = "He" . $Inst->say;
$ Say變量只能在該函數中訪問 - 必須首先將其轉換爲屬性 – hex4
您將需要在你的函數聲明變種,如
class MyClass {
public $say;
function Talk() {
$this->say = "something";
}
}
然後
$Said = "He ".$Inst->$say;
OOP的最佳做法,是永遠不會有你班上的公共財產。操作類的屬性的最佳方法是使用單獨的方法來返回屬性的值並設置屬性的值。 所以
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;
}
}
所以現在,因爲他們是公衆可以訪問您的私人性質,並盡一切neccessary檢查,沒有得到錯誤值存儲在其中。 就像是:
$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方法私有的,但隨後的代碼的第二部分將無法工作。
此工作,非常感謝! :D – max0005
只是澄清一點:這會使'$ say'成爲_object_屬性。像'static $ say ='Something';'這樣的聲明會使其成爲_class_屬性。 – Ryan