基本上,我想使用一個密鑰,並且能夠從另一個類調用一個類的函數。圍繞多個類使用密鑰
也許我會提供的例子將給出我的意思更好的解釋。我想到const
,但我不能從那裏修改它。
我想過解析鍵作爲構造函數,我只是想知道是否有其他更好的方法來做它,因爲它將用於多個類。
Class1.php
<?PHP
class Class1 {
public $key;
public function setKey($k)
{
$this->key = $k;
}
public static function getKey()
{
return $this->key; // this won't work because static
}
}
?>
Class2.php
<?PHP
class Class2 {
public function __construct()
{
echo Class1::GetKey(); // Need to retrieve the key here
}
}
?>
的index.php
<?PHP
require_once("Class1.php");
require_once("Class2.php");
$c1 = new Class1();
$c1->setKey("thekey");
$c2 = new Class2();
?>
謝謝你,我認爲這就是我要做的。 – Nathan 2014-11-02 13:50:07