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?

7
  • pass it to the constructor? Commented Jun 13, 2013 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. Commented Jun 13, 2013 at 18:33
  • an example is below, if you want to get the variable, you can make an assessor method for it Commented Jun 13, 2013 at 18:35
  • 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. ;) Commented Jun 13, 2013 at 19:18
  • 2
    When did StackOverflow become Wikipedia? ;) Commented Jun 13, 2013 at 19:22

2 Answers 2

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);.

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

8 Comments

what does that accomplkish? then you can pass it to the constructor when creating the object... granted... but thats about it isnt it?
i dont think that is what you want. see comment above.
@TheSurrican Why do you think this is wrong. I have downvoted your answer because using global is terrible practice. So please don't.
@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.
@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.
|
-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;
  }
}

3 Comments

You may want to read this: stackoverflow.com/questions/11923272/…
@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.
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.

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.