在php中访问嵌套在类内函数中的变量

在php中访问嵌套在类内函数中的变量,php,Php,下面是我的代码的精简版本,其中包含main类和main函数 我需要获取用户输入的'userName'的值,以便在'main function'中使用它,尝试将'userName'内部的'myForm'设置为全局,但没有获取该值 'userName'的值是否可以在'mainClass'外部使用,以便我可以在任何地方使用它 class mainClass { function myForm() { echo '<input type="text" value="'.$use

下面是我的代码的精简版本,其中包含main
和main
函数

我需要获取用户输入的
'userName'
的值,以便在
'main function'
中使用它,尝试将
'userName'
内部的
'myForm'
设置为全局,但没有获取该值

'userName'
的值是否可以在
'mainClass'
外部使用,以便我可以在任何地方使用它

 class mainClass {

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

 }   
 function mainFunction () {
    $myArray = array (

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

      ) 
 }
class类main类{
函数myForm(){
回声';
}
}   
函数main函数(){
$myArray=array(
'child_of'=>$GLOBALS['userName']
) 
}
class mainClass{
公共$username;
函数构造($username){
$this->username=$username;
}
函数myForm(){
回声';
}
}
函数main函数(){
$myArray=array(
'child_of'=>this->username;
);
}

下面的代码是get\u实例方法的一个非常精简的版本。因此,在您的情况下,您可以在开头的某个地方使用以下代码:

/** 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();
class MainClass
{

    private $_userName;

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

    public function myForm()
    {
        echo '<input type="text" value="'.$this->_userName.'" />';
    }
}
“userName”的值是否可以在“mainClass”之外使用,以便我可以在任何地方使用它

 class mainClass {

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

 }   
 function mainFunction () {
    $myArray = array (

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

      ) 
 }

首先,您需要像这样定义一个类属性

class MainClass
{

    private $_userName;

    public function myForm()
    {
        echo '<input type="text" value="'.$this->_userName.'" />';
    }
}
$main = new MainClass();
$userName = $main->getUserName();
请注意,您需要MainClass的一个实例

我建议你从简单的概念开始,并确保你100%理解这一点。我还建议避免使用全局变量和更复杂的静态方法逻辑。尽量使它简单

亲切问候,,
Victor

从函数返回它!?将用户名传递给main函数($username)?如果不将标记的参数传递给类函数,您可以为类指定
常量
而不是
全局变量
,我仍然无法在MyArrayGeting中访问main函数内的username变量错误警告:array\u merge()[function.array merge]:参数#2不是数组