2013-03-03 61 views
2

這裏是削減我的代碼版本與主class和主function訪問嵌套函數中的類中的PHP變量

我需要得到的'userName'值由用戶輸入使用它在'mainFunction'內,試圖在'myForm'全球範圍內製作'userName',但這並沒有得到有價值的結果。

可以將'userName'的值設爲'mainClass',這樣我就可以在任何地方使用它了嗎?

class mainClass { 

    function myForm() { 
     echo '<input type="text" value="'.$userName.'" />'; 
    } 

} 
function mainFunction() { 
    $myArray = array (

     'child_of' => $GLOBALS['userName'] 

    ) 
} 
+0

'從函數return'吧!? – deceze 2013-03-03 09:57:07

+0

將你的用戶名傳給你的mainFunction($ username)? – MIIB 2013-03-03 09:57:43

+0

如果你不打算將標記的參數傳遞給類函數,你可以用'常量'代替你的類的'全局變量' – tnanoba 2013-03-03 09:57:51

回答

1
class mainClass { 
    public $username; 
    function __construct($username){ 
     $this->username = $username; 
    } 

    function myForm() { 
     echo '<input type="text" value="'.$this->userName.'" />'; 
    } 

} 

function mainFunction() { 
    $myArray = array (
     'child_of' => $this->username; 
    ); 
} 
+0

我仍然無法訪問myArray中的mainFunction中的userName變量 – Grundizer 2013-03-03 10:09:25

+0

@Grundizer怎麼樣noww – 2013-03-03 10:12:01

+0

出現錯誤警告:array_merge()[function.array-merge]:參數#2不是數組 – Grundizer 2013-03-03 10:22:18

-1

在下面的代碼是Codeigniter get_instance方法的非常最小化版本。所以你的情況,你可以在一開始有地方這樣的代碼:

/** Basic Classes to load the logic and do the magic */ 

class mainInstance { 

    private static $instance; 

    public function __construct() 
    { 
     self::$instance =& $this; 
    } 

    public static function &get_instance() 
    { 
     return self::$instance; 
    } 
} 

function &get_instance() 
{ 
    return mainInstance::get_instance(); 
} 

new mainInstance(); 
/** ----------------------------------------------- */ 

,然後你可以這樣創建全局類:

class customClass1 { 

    public $userName = ''; 

     function myForm() { 
      return '<input type="text" value="'.$this->userName.'" />'; 
     } 

} 

/** This is now loading globally */ 
$test = &get_instance(); 
//If you choose to actually create an object at this instance, you can call it globally later 
$test->my_test = new customClass1(); 
$test->my_test->userName = "johnny"; 

/** The below code can be wherever in your project now (it is globally) */ 
$test2 = &get_instance(); 
echo $test2->my_test->myForm()."<br/>"; //This will print: <input type="text" value="johnny" /> 
echo $test2->my_test->userName; //This will printing: johnny 

由於這是目前全球範圍內,你甚至可以創建自己的像這樣的功能:「用戶名」的

function mainFunction() { 
    $tmp = &get_instance(); 
    return $tmp->my_test->userName; 
} 

echo mainFunction(); 
1

能值可從側面「mainClass」,這樣我可以在任何地方使用它?

是。

首先,你需要像這樣定義

class MainClass 
{ 

    private $_userName; 

    public function myForm() 
    { 
     echo '<input type="text" value="'.$this->_userName.'" />'; 
    } 
} 

看你怎麼myForm會()方法中訪問該物業的類屬性。

然後,你需要定義getter方法此屬性(或者你可以讓財產公開)是這樣的:

class MainClass 
{ 

    private $_userName; 

    public function getUserName() 
    { 
     return $this->_userName; 
    } 

    public function myForm() 
    { 
     echo '<input type="text" value="'.$this->_userName.'" />'; 
    } 
} 

您可以訪問用戶名物業這樣

$main = new MainClass(); 
$userName = $main->getUserName(); 

注意你需要一個MainClass類的實例。

我建議你從簡單的概念開始,並確保你理解這100%。另外我會建議避免使用靜態方法使用全局變量和更復雜的邏輯。儘量讓它儘可能簡單。

此致, 維克多