zadubz
zadubz

Reputation: 1301

Access a variable nested in a function inside a class in php

Here is a cut down version of my code with the main class and main function

I need to get the value of 'userName' that input by the user to use it inside 'mainFunction', tried making the 'userName' inside 'myForm' global but that didn't get the value out.

can value of 'userName' be available out side 'mainClass' so that I can use it anywhere?

 class mainClass {

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

 }   
 function mainFunction () {
    $myArray = array (

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

      ) 
 }

Upvotes: 2

Views: 300

Answers (3)

Victor Smirnov
Victor Smirnov

Reputation: 3780

can value of 'userName' be available out side 'mainClass' so that I can use it anywhere?

Yes.

First, you need to define a class property like this

class MainClass
{

    private $_userName;

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

Look at how you access this property inside the myForm() method.

Then you need to define getter method for this property (or you can make the property public) like this:

class MainClass
{

    private $_userName;

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

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

You can access user name property like this

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

Note that you need an instance of the MainClass class.

I suggest you to start with simple concept and make sure you understand this 100%. Also I would suggest avoid using global variables and more complicated logic with static methods. Try to make it as simple as possible.

Warm regards, Victor

Upvotes: 1

John Skoubourdis
John Skoubourdis

Reputation: 3259

The below code is a very minimized version of the Codeigniter get_instance method. So in your case you can have somewhere at the beginning this code:

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

and then you can create your global Classes like this:

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

As this is now globally you can even create your own function like this:

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

echo mainFunction();

Upvotes: -1

user1236473
user1236473

Reputation:

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

Upvotes: 1

Related Questions