8

I'm very new to PHP classes so forgive me if the answer is really obvious. I'm trying to figure out how to use a variable defined outside of a class inside of a class. Here is a very crude example

$myVar = 'value';

class myClass {
  private $class_var = $myVar;
  //REST OF CLASS BELOW
}

I know the above doesn't work, but how can I use the external $myVar inside the class?

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
B_Chesterfield
  • 109
  • 1
  • 1
  • 4
  • pass it to the constructor? – Dave Chen Jun 13 '13 at 18:32
  • Could you give me an example? As I mentioned I'm very new to classes so I'm still learning the ins and outs. – B_Chesterfield Jun 13 '13 at 18:33
  • an example is below, if you want to get the variable, you can make an assessor method for it – Dave Chen Jun 13 '13 at 18:35
  • possible duplicate of [Use global variables in a class](http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class) – IMSoP Jun 13 '13 at 19:05
  • 2
    For the record, it was David Chen who first made the edit in question, not PeeHaa. PeeHaa made an accidental edit, then rolled it back to the one David had made. Then the 'war' ensued. I agree that the extra wording isn't necessary and David was right to remove it, but I'd prefer not to see these types of edits cause lots of rollbacks. I think the two should stop doing the Tango. ;) – Andrew Barber Jun 13 '13 at 19:18
  • one last edit! _Do it for Johnny!_ – Carrie Kendall Jun 13 '13 at 19:21
  • 2
    When did StackOverflow become [Wikipedia? ;)](http://en.wikipedia.org/wiki/Wikipedia:Edit_warring) – IMSoP Jun 13 '13 at 19:22

2 Answers2

14

Try this:

$myVar = 'value';

class myClass {
  private $class_var;

  public function __construct($myVar) {
    $this->class_var=$myVar;
  }

  //REST OF CLASS BELOW
}

When declaring the class, you will need to pass $myVar like so, $myClass = new myClass($myVar);.

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • 1
    what does that accomplkish? then you can pass it to the constructor when creating the object... granted... but thats about it isnt it? – The Surrican Jun 13 '13 at 18:36
  • i dont think that is what you want. see comment above. – The Surrican Jun 13 '13 at 18:39
  • 1
    @TheSurrican Why do you think this is wrong. I have downvoted your answer because using `global` is terrible practice. So please don't. – PeeHaa Jun 13 '13 at 18:44
  • 1
    @TheSurrican I don't think we should be condoning or supporting OP (*or new users in general*) to throw everything out the window and just `global` it. DI is a strategy for a reason. – Dan Lugg Jun 13 '13 at 18:44
  • i think it really depends on the application. nobody even ever said that an object should be created from that class and not static functions are used. – The Surrican Jun 13 '13 at 19:18
  • i alwasy preferred the open mind of the php world in comparision to unqestionable java dogmatics... true that the alternative of DI should be pointed out. but global has its legibility. also i would like you to consider the scenario that the change should be made to already written code. maby there are objectes created at thousands of places in existing code. and all you want is to use a new variable that is already defined in that funciton. would you want to change thousands of lines of code? ok now you can argue that DI should have been used. maby it has? and this is new? – The Surrican Jun 13 '13 at 19:22
  • 2
    @TheSurrican Your suggestion that the class might be static is not borne out by the code given - there is no `static` keyword, and giving an object property an initial value is the responsibility of a constructor, as both your answer and DaveChen's show. I do agree that not mentioning `global` would be a mistake, but it should probably always come with a few caveats - for instance, as you say, that it could be a way of migrating older code that cannot trivially be de-globalised. – IMSoP Jun 13 '13 at 19:32
  • put it this way, i totally agree – The Surrican Jun 13 '13 at 19:33
-3

Every function has its own "scope". You can override that by declaring a variable as global inside the function like this:

$myVar = 'value';

class myClass {
  public function __construct() {
    global $myVar;
    $this->class_var=$myVar;
  }
}

this will set variable in the object instance.

However be advised that you can directly use it in functions without the need to set it as class variable like this:

$myVar = 'value';

class myClass {
  public function myfunction() {
    global $myVar;
    echo $myVar;
  }
}
The Surrican
  • 29,118
  • 24
  • 122
  • 168
  • 2
    You may want to read this: http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class/11923384#11923384 – PeeHaa Jun 13 '13 at 18:45
  • @PeeHaa埽 That is such a thorough answer, I have voted for this question to be closed as a duplicate of that one rather than repeating the discussion. – IMSoP Jun 13 '13 at 19:09
  • The first half of this answer, with the caveat of "this is not considered the best solution, but can be useful for integrating with existing code", is a useful addition. The second half is just encouraging bad practice - at least if the `global` is constrained to the constructor, you are limiting the number of references to that global variable, and it can even be migrated to DI at some point in the future. – IMSoP Jun 13 '13 at 19:37