0

I am trying to pass a JSON response from an external API to a class in a different file. Somehow it's not storing in the class. I've tried var_dumping the class like this:

$user->country_ip = $country_ip; die(var_dump($user->country_ip));

It somehow doesn't work. But, when I try to vardump the actual variable like this:

die(var_dump($country_ip));

Then it does work, so the variable is fine. I'm very confused, does anyone have an answer to this? It's midnight here, it might be something small?

4
  • your user class has the public variable country_ip? Commented Jul 16, 2019 at 23:43
  • What do you mean? The variable itself is global and accessible throughout the entire file Commented Jul 16, 2019 at 23:45
  • I've added an example for you, Have a look there Commented Jul 16, 2019 at 23:47
  • Can you be more specific about what "doesn't work" means? I used that same code and it dumped the value as expected. Commented Jul 17, 2019 at 0:01

1 Answer 1

1

A minimalist example will be like this,

class User
{
  public $country_ip;

  public function getCountryIp()
  {
    return json_decode($this->country_ip, true);
  }
}

$country_ip = '192.168.2.227';
$user = new User();
$user->country_ip= json_encode($country_ip);
var_dump($user->getCountryIp()); //It will print "192.168.2.227"

DEMO: https://3v4l.org/YSk9X

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

2 Comments

The function is not within the class, it's actually in a different file. Does that make a difference? I have obviously created the class, etc. And it works with all my other variables which I post towards the file. It just doesn't seem to work with this variable from a new function?
Nvm! Your answer worked! Thanks a lot man, I just overlooked the public part and forgot to do it for the country_ip variable

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.