0

I try to initialize an array using another array in php class. Here is the code:

 <?php class test
{
    var $nodeDomain = array
        ("gd88" =>"10.10.104.88", "gd02" =>"10.10.104.2");
    var $node = array
        ("x86-mysql" =>$nodeDomain['gd88'],
         "x86-hbase" =>$nodeDomain['gd02']);

    function show ()
    {
        print_r($node);
    }
}
?>

I got this error: Parse error: syntax error, unexpected T_VARIABLE in /root/workspace/php/array.php on line 6

But when I run the code without using class it works fine. I mean I run the following code:

var $nodeDomain = array
    ("gd88" =>"10.10.104.88", "gd02" =>"10.10.104.2");
var $node = array
    ("x86-mysql" =>$nodeDomain['gd88'],
     "x86-hbase" =>$nodeDomain['gd02']); 

I am not quite clear about the difference of php class and php script. Can anyone explain this?

Thanks.

3
  • While you're at it: var $foo is deprecated, you must use (private|protected|public) since php 5. Also, print_r($node) won't work because it's in the objects' scope, not in the method scope. Use print_r($this->node) instead. Commented Jul 22, 2011 at 8:23
  • For further reading, have a look at Properties ... This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. Commented Jul 22, 2011 at 8:26
  • Thanks Berry for the kind help Commented Jul 24, 2011 at 6:38

3 Answers 3

2

You can not use another variables when declaring class members. Try to initialize them in constructor.

<?php class test
{
    var $nodeDomain;
    var $node;

    public function __construct() {
       $this->nodeDomain = array("gd88" =>"10.10.104.88", "gd02" =>"10.10.104.2"); 
       $this->node = array("x86-mysql" =>$this->nodeDomain['gd88'],
         "x86-hbase" =>$this->nodeDomain['gd02']);
    }
    function show ()
    {
        print_r($node);
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe change $nodeDomain to $this->nodeDomain. As $nodeDomain is non-existent in the scope of the constructor.
0

Try put those array-initialazing to the constructor of the test class

Comments

0

You just can't reference variables in the field declarations. Where should this variable come from anyway? There are no local variables and no way to position a global statement. (Of course superglobals could work but that's obviously not implemented ;-)) Instead you can do something like this:

<?php class test
{
    var $nodeDomain = array
        ("gd88" =>"10.10.104.88", "gd02" =>"10.10.104.2");
    var $node;

    function __construct()
    {
      $this->node = array
        ("x86-mysql" =>$nodeDomain['gd88'],
         "x86-hbase" =>$nodeDomain['gd02']);
    }
    function show ()
    {
        print_r($node);
    }
}
?>

Beware that $nodeDomain must be in the scope of the constructor somehow. Either it is a global variable, so you need a global $nodeDomain statement before the assignment or you can pass $nodeDomain as constructor argument.

1 Comment

Thanks. I realize why I could not do that. Great help!

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.