2008-12-25 116 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 

?>