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
精彩评论