2013-03-03 102 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%。另外我会建议避免使用静态方法使用全局变量和更复杂的逻辑。尽量让它尽可能简单。

此致, 维克多