0

I have an array. but I don't know how to access it inside a class.

below is my sample code.

<?php

$value[0]=11;
$value[1]=22;
$value[2]=33;

class test {
    var $code1,$code2,$code3;

    function __construct() {
            $this->$code1 = $value[0];
            $this->$code2 = $value[1];
            $this->$code3 = $value[2];
            echo $code1;
        }
}

4 Answers 4

1

Read this.

And you can do any one of the following:

Pass the value into the constructor as a parameter (recommended option)

<?php

$value[0]=11;
$value[1]=22;
$value[2]=33;

class test {
    var $code1,$code2,$code3;

    function __construct($value) {
            $this->code1 = $value[0];
            $this->code2 = $value[1];
            $this->code3 = $value[2];
            echo $this->code1;
        }

}

$obj = new test($value);

?>

Use the $GLOBALS array (docs)

<?php

$value[0]=11;
$value[1]=22;
$value[2]=33;

class test {
    var $code1,$code2,$code3;

    function __construct() {
            $this->code1 = $GLOBALS['value'][0];
            $this->code2 = $GLOBALS['value'][1];
            $this->code3 = $GLOBALS['value'][2];
            echo $this->code1;
        }

}

$obj = new test;

?>

Use the global keyword (docs)

<?php

$value[0]=11;
$value[1]=22;
$value[2]=33;

class test {
    var $code1,$code2,$code3;

    function __construct() {
            global $value;
            $this->code1 = $value[0];
            $this->code2 = $value[1];
            $this->code3 = $value[2];
            echo $this->code1;
        }

}

$obj = new test;

?>

NOTES

I have corrected a couple of errors above.

You should use $this->code1 instead of $this->$code1. The second version is valid syntactically, but means something else. Consider the following example:

 class myClass {

   public $myVar = "My Var";
   public $anotherVar = "Another Var";

   function __construct () {

     // creates a local variable to the constructor, called $myVar
     // does NOT overwrite the value defined above for the object property
     $myVar = "anotherVar";

     echo $myVar; // echoes 'anotherVar'
     echo $this->myVar; // echoes 'My Var'
     echo $this->$myVar; // echoes 'Another Var'

   }

 }

Also, the above example illustrates the reason why you should use echo $this->code1; and not simply echo $code1;

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this:

class test
{
    var $code1,$code2,$code3;

    function __construct($input)
    {
        $this->code1 = $input[0];
        $this->code2 = $input[1];
        $this->code3 = $input[2];
    }
}

[...]

$value[0]=11;
$value[1]=22;
$value[2]=33;

$test = new test($value);

Comments

0

Global variables aren't visible inside functions, unless you use the global keyword. e.g.:

// Global variable
$x = 5;

// Won't affect the global
function foo()
{
    $x = 3;
}

// Will affect the global
function bar()
{
    global $x;
    $x = 2;
}

Note

However, in general, it's not a good idea to use global variables like this. It introduces dependencies, and makes your code harder to test and to debug. I suggest you pass the variable in as an argument to your constructor.

4 Comments

IMHO, global really isn't something you should be advertising.
@CodeCaster: Indeed; I've explicitly explained that this is a bad idea.
Thanks a lot Oli, Dave, Alfwed, CodeCaster for your help. So what i learnt from this is I should pass the values to avoid unnecessary dependencies and use $this->var instead of $this->$var to avoid overwriting. This is great. Thank u all again
let's say we have $config variable that stores variables from config.php file? what should we do now? pass it for every constructer & make server ram usage great?
0
class test { 
    private $code1,$code2,$code3;

    function __construct($value) {
        $this->code1 = $value[0];
        $this->code2 = $value[1];
        $this->code3 = $value[2];
        echo $this->code1;
    }
}

Usage :

$test = new Test($value);

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.