2

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']

      ) 
 }
3
  • return it from a function!? Commented Mar 3, 2013 at 9:57
  • pass your username to your mainFunction($username)? Commented Mar 3, 2013 at 9:57
  • You can you constant instead of global variable for your class, if you aren't going to pass marked parameter to the class function Commented Mar 3, 2013 at 9:57

3 Answers 3

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;
    );
 }
Sign up to request clarification or add additional context in comments.

2 Comments

I still can't access the userName variable inside mainFunction in myArray
getting an error Warning: array_merge() [function.array-merge]: Argument #2 is not an array
1

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.