简体   繁体   English

变量未传递给类(PHP)

[英]Variables not passing to class (PHP)

I am trying to pass a JSON response from an external API to a class in a different file. 我试图将JSON响应从外部API传递到其他文件中的类。 Somehow it's not storing in the class. 不知何故,它没有存储在类中。 I've tried var_dumping the class like this: 我已经尝试过var_dumping这样的类:

$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? 现在是午夜,可能有点小?

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 演示: https : //3v4l.org/YSk9X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM