题
我有2类,主要和扩展。我需要在扩展的类使用主VARS。
<?php
class Main {
public $vars = array();
}
$main = new Main;
$main->vars['key'] = 'value';
class Extended extends Main { }
$other = new Extended;
var_dump($other->vars);
?>
谁,我可以做到这一点?
没有有效,例如:
<?php
class Extended extends Main {
function __construct ($main) {
foreach ($main as $k => $v) {
$this->$k = $v;
}
}
}
?>
我需要一些溶液更透明和有效的:)
解决方案
修改强>:这可以用更好的解决控制反转(IOC)和依赖注入(DI)。如果你用自己的框架或一个没有的依赖注入容器的尝试联盟/集装箱
下面留下愚蠢的答案的历史来回答。
正确的方法我的数字。
<?php
class Config {
protected $_vars = array();
protected static $_instance;
private function __construct() {}
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function &__get($name) {
return $this->_vars[$name];
}
public function __set ($name, $value) {
$this->_vars[$name] = $value;
}
}
$config = Config::getInstance();
$config->db = array('localhost', 'root', '');
$config->templates = array(
'main' => 'main',
'news' => 'news_list'
);
class DB {
public $db;
public function __construct($db)
{
$this->db = $db;
}
public function connect()
{
mysql_connect($this->db[0], $this->db[1], $this->db[2]);
}
}
$config = Config::getInstance();
$db = new DB($config->db);
$db->connect();
class Templates {
public $templates;
public function __construct($templates)
{
$this->templates = $templates;
}
public function load ($where) {
return $this->templates[$where];
}
}
$config = Config::getInstance();
$templates = new Templates($config->templates);
echo $templates->load('main') . "\n";
其他提示
这将是容易地能够以简单的构造
<?php
class One {
public static $string = "HELLO";
}
class Two extends One {
function __construct()
{
parent::$string = "WORLD";
$this->string = parent::$string;
}
}
$class = new Two;
echo $class->string; // WORLD
?>
我意识到这是超级老了,但如果任何人需要一个线索...
你有没有使用静态变量考虑?
在PHP OOP设计模式是这样的,静态声明在变量a的父类保持相同的子类,太
例如...
<?php
class A {
public static $test = 'a';
public function test() {
echo 'Test is: '.self::$test;
}
}
class B extends A {
public static $test = 'b';
}
$obj = new B;
$obj->test();
?>
运行这段代码(在PHP 5.3-我敢肯定,这对于其他版本一样,太)会给你以下结果:
<强> 测试为:a 强>
从我能在你的OP收集,你正在寻找的的父类变量的方式的保持 - 即使是在扩展类。这解决了这个问题。
要公开呼吁变量的类范围之外(即这里你通常写的 $ obj->瓦尔的),你需要在引用self::$variable_name
父类来创建一个功能使得它可以抛出该变量回到其利用要么类,或扩展它的任何其它类的代码。
例如,是这样的:
public function get_variable() {
return self::$variable;
}
您还可以创建一个神奇的方法,将动态倒退根据你问的实例什么自我:: $变量 - 即一个方法或变量。你可以电汇代码倒退自我::在任何情况下$变量等价的。
读取 http://php.net/manual/en/language。 oop5.magic.php ,获取有关的各种神奇的方法,让你做这样的东西更多信息。
在OP有点神秘,所以我不知道如果这就是你想要的到底是什么,但我没有看到其他人在这里引用静态变量,所以想我会附和 - !希望它能帮助
我认为你需要了解你想要什么更加清晰。试想一下,你有两个主要和扩展的几个实例。他们都应该是指相同的数据,因此,如果您更改其中的任何一个数据,他们都受到影响?
如果是这样,则一种方法是使用一个静态变量,该变量是联系在一起的类,而不是单独的实例。另一种方法是创建一个单独的对象(不同类的)来存储你的数据,并在创建时将它传递给主和扩展类。它们可以各自存储对它的引用,e.g:
class Main {
public $data;
function __construct(Data $data) {
$this->data = $data;
}
}
class Extended extends Main {}
$ourData = new Data();
$ourData->vars = array(1,2,3);
$main = new Main($ourData);
$other = new Extended($ourData);
如果没有,那么你想要的数据的副本,而不是引用相同数据。在这种情况下,你的第二个例子是接近的,虽然我不会只是一味的所有成员进行复制。
oooooooooooooooooooooooooooooooooooooooooooooooooooooh
你想这样的:
class Config
{
private $m_vars = array();
function __get($name)
{
return $this->m_vars[$name];
}
function & __set($name, $value) // << the & is important!
{
$this->m_vars[$name] = $value;
}
}
class Db
{
funciton __construct(Config $config)
{
$this->Connect($config->host, $config->user, $config->pass, $config->db);
}
}
$config = new Config();
$config->host = "localhost";
...
$db = new Db($config);
:)
编辑:
另外,你可以这样做:
class Templator
{
private $m_config = null;
function __construct($config)
{
$this->m_config = $config;
}
function PrintTemplate($name)
{
echo file_get_contents($this->m_config->template_path . $name);
}
}
$config->template_path = "./templates/";
$temp = new Templator($config);
$temp->PrintTemplate("index.html");