Basically, I want to use a key and be able to call a function in a class from another class.
Maybe the example I will provide will give a better explanation as to what I mean. I thought of const
but I would not be able to modify it from there.
I thought about parsing the key as a constructor, I was just wondering if there is any other better way of doing it since it'll be used in multiple classes.
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();
?>