What are the potential drawbacks of extending a class like "Registry" with another class like "Config" in PHP?
Extending a class like "Registry" with another class like "Config" can lead to a tight coupling between the two classes, making the code harder to maintain and understand. Instead of extending the "Registry" class with "Config", it is better to create a separate class that encapsulates the configuration logic and use it within the "Registry" class as needed.
class Registry {
private $data = [];
public function set($key, $value) {
$this->data[$key] = $value;
}
public function get($key) {
return $this->data[$key];
}
}
class Config {
private $configData = [];
public function setConfig($key, $value) {
$this->configData[$key] = $value;
}
public function getConfig($key) {
return $this->configData[$key];
}
}
// Usage
$registry = new Registry();
$config = new Config();
$config->setConfig('db_host', 'localhost');
$config->setConfig('db_user', 'root');
$registry->set('config', $config);
echo $registry->get('config')->getConfig('db_host'); // Output: localhost