开发者

Initialize a ComplexType variable inside a Class (SoapClient)

开发者 https://www.devze.com 2023-01-28 03:27 出处:网络
Hey everyone, I am trying to consume a WebService using PHP SoapClient. Now, everything is working fine until I try to use a ComplexType variable.

Hey everyone, I am trying to consume a WebService using PHP SoapClient. Now, everything is working fine until I try to use a ComplexType variable. I am getting a NullPointerException from the web service.

Here is an example ComplexType variable's class:

class A {
public $var1 = 0; //int
public $var2; //Obj开发者_高级运维ect: B
}

Now, I am making an object like this:

$a = new A();
$a->var2 = new B();

But, this has to be done every time I make an object of A. Is there a method to initialize $var2 in class A, without using the constructors for it, like we are doing for $var1?


Yes, is possible if using PHP overloading

class A
{
  public $var1 = 0;
  /* note $var2 cannot declare here */

  public function __get($name)
  {
    switch ($name)
    {
      case 'var2':
        return new B();
        break;
    }
  }
}

class B
{
  public $var3 = 1;
  public function __construct()
  {
    $this->name = 'this is class b';
  }
}

$a = new A();
var_dump( $a->var2 );
var_dump( $a->var2->var3 );
var_dump( $a->var2->name );

Is called using $a->var2 instead of $a->$var2

0

精彩评论

暂无评论...
验证码 换一张
取 消