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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
roamcel
  • 645
  • 1
  • 8
  • 17

5 Answers5

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.

Community
  • 1
  • 1
Nick Shvelidze
  • 1,564
  • 1
  • 14
  • 28
  • 3
    Please note that this is considered a bad practice (as well as `$GLOBALS`). – kapa Dec 13 '11 at 11:41
  • 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. – kapa Dec 13 '11 at 11:45
  • 1
    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. – roamcel Dec 13 '11 at 11:45
  • 6
    Can we have an edit that points to a best practice alternative? – Evan Nov 26 '13 at 18:29
  • @Evan, "best practice" changes with time, culture, and use case. Facts don't. – Pacerier Mar 04 '15 at 07:57
3

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

macjohn
  • 1,755
  • 14
  • 18
1

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

Pupil
  • 23,834
  • 6
  • 44
  • 66
jbrtrnd
  • 3,815
  • 5
  • 23
  • 41
0
 $GLOBALS['myarray'] =  array('firstval','secondval');

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

djot
  • 2,952
  • 4
  • 19
  • 28
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 ;
          }    
       }   
    }

    ?>
Amelia
  • 320
  • 6
  • 16
  • a bit too complex for what can be otherwise accomplished with one line :) – roamcel Dec 16 '11 at 15:56
  • @Amelia is this a solution for my question here http://stackoverflow.com/questions/33613200/php-define-variable-in-global-scope-and-use-it-in-a-class-function ? Thank you – thednp Nov 09 '15 at 17:22