9

I have the following script

myclass.php

<?php

$myarray = array('firstval','secondval');

class littleclass {
  private $myvalue;

  public function __construct() {
    $myvalue = "INIT!";
  }

  public function setvalue() {
    $myvalue = $myarray[0];   //ERROR: $myarray does not exist inside the class
  }
}

?>

Is there a way to make $myarray available inside the littleclass, through simple declaration? I don't want to pass it as a parameter to the constructor if that was possible.

Additionally, I hope that you actually CAN make global variables visible to a php class in some manner, but this is my first time facing the problem so I really don't know.

5
  • 3
    Why? This is 180º away from common sense. And did you read the documentation at all? Commented Dec 13, 2011 at 11:32
  • 3
    Warning: using global variables will quickly turn your code into an unmaintainable mess. Consider passing it as a parameter either to the constructor, or setvalue(), whichever is logical in your app. Commented Dec 13, 2011 at 11:39
  • 1
    I did not 'read the documentation' since I did not even know what to look for. Thanks for the 'why global' suggestion. Commented Dec 13, 2011 at 11:39
  • I agree with @TomalakGeret'kal: You're doing it wrong! Commented Dec 13, 2011 at 11:39
  • 1
    Thanks for the solution approach bazmegakapa Commented Dec 13, 2011 at 11:43

5 Answers 5

18

include global $myarray at the start of setvalue() function.

public function setvalue() {
    global $myarray;
    $myvalue = $myarray[0];
}

UPDATE:
As noted in the comments, this is bad practice and should be avoided.
A better solution would be this: https://stackoverflow.com/a/17094513/3407923.

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

5 Comments

Please note that this is considered a bad practice (as well as $GLOBALS).
I'd say simply setvalue($value), the array should not be relevant to the class. It just receives a value. But we do not know the app logic.
I'm marking this as the solution since it's technically the correct answer to my question. Anybody reading this in the future please note that such approach should be avoided if possible.
Can we have an edit that points to a best practice alternative?
@Evan, "best practice" changes with time, culture, and use case. Facts don't.
3

in a class you can use any global variable with $GLOBALS['varName'];

Comments

1

Construct a new singleton class used to store and access variables you want to use.

Comments

0
 $GLOBALS['myarray'] =  array('firstval','secondval');

In the class you just might use $GLOBALS['myarray'].

Comments

0

Why dont you just use the getter and setter for this?

<?php

    $oLittleclass = new littleclass ;
    $oLittleclass->myarray =  array('firstval','secondval');

    echo "firstval: " . $oLittleclass->firstval . " secondval: " . $oLittleclass->secondval ;

    class littleclass 
    {
      private $myvalue ;
      private $aMyarray ;

      public function __construct() {
        $myvalue = "INIT!";
      }

      public function __set( $key, $value )
      {
        switch( $key )
        {
          case "myarray" :
            $this->aMyarray = $value ;
          break ;
        }
      }

       public function __get( $key )
       {
          switch( $key )
          {
            case "firstval" :
              return $this->aMyarray[0] ;
            break ;
            case "secondval" :
              return $this->aMyarray[1] ;
            break ;
          }    
       }   
    }

    ?>

2 Comments

a bit too complex for what can be otherwise accomplished with one line :)
@Amelia is this a solution for my question here stackoverflow.com/questions/33613200/… ? Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.