2008-12-25 114 views
6

我有2个类,main和extended。我需要在扩展类中使用主要变量。在PHP的扩展类中使用父变量

<?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; 
    } 
    } 
} 
?> 

我需要一些解决方案更加透明和高效的:)

+0

我不明白这一点。我会更乐意回答你的问题,但还不够清楚。你想构建“扩展”像($ obj = new Extended($ main))或者你在寻找静态变量吗? – nlaq 2008-12-25 23:06:38

回答

4

编辑:这可以更好的与控制反转的(IOC)能得到解决, 依赖注入(DI)。如果您使用自己的框架或一个没有依赖注入容器尝试League/Container

回答下面的左边作为愚蠢的答案的历史。


我认为正确的方法。

<?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"; 
+0

我想谁也不知道PHP和改装成我下来可以解释为什么这个家伙。 – OIS 2008-12-25 22:11:24

+0

我不会downmod,但你真的缺乏OOP设计领域。你永远不应该扩展配置类 - 配置对象应该包含在消耗它们的对象中 - 不能扩展到其他类中。对不起,但这简直是愚蠢的。 – nlaq 2008-12-25 23:47:16

1

我认为你需要更清楚你想要什么。想象一下你有几个Main和Extended的实例。他们是否都应该引用相同的数据,以便在其中任何一个数据中更改数据时,它们都会受到影响?

如果是这样,那么一种方法是使用一个静态变量,它绑定到类而不是单个实例。另一种方法是创建一个单独的对象(不同类)来存储数据,并在创建时将它们传递给Main和Extended类。他们可以在每个存储对它的引用,例如:

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); 

如果没有,那么你想要的数据的副本,而不是引用相同数据。在这种情况下,你的第二个例子更接近,但我不会盲目地复制所有成员。

0

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"); 
2

我意识到这是超旧的,但在其他人需要线索的情况下...

你有没有考虑过使用静态变量?

PHP的面向对象的设计模式是这样的,静态声明在类变量保持在孩子类一样,太。

例如...

<?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-我敢肯定,这对于其他版本一样,太)会给你以下结果:

测试是:一个

从我能在你的OP收集,你正在寻找的父类变量办法仍然存在 - 即使是在扩展类。这解决了这个问题。

要公开调用类范围之外的变量(即这里你通常写$ obj->瓦尔),你需要在引用self::$variable_name以便它可以在父类来创建一个功能将该变量放回到使用该类的代码或任何其他扩展它的类。

例如,像:

public function get_variable() { 
    return self::$variable; 
} 

您还可以创建一个神奇的方法,将根据你的要求实例动态丢回自我:: $变量 - 即一个方法或变量。在任何情况下,你都可以连接代码以返回self :: $变量。

阅读http://php.net/manual/en/language.oop5.magic.php上的各种神奇的方法,让你做这样的东西更多信息。

的OP是有点神秘,所以我不知道,如果这正是你想要的,但我没有看到其他人在这里引用静态变量,所以想我会附和 - 希望它帮助!

6

这将是很容易能够以简单的构造

<?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 

?>