I have the following class,e.g.
class A{
public var1;
public var2;
}
now, I want to create an object of this class, and I do the following:
$a = new A();
$a->var1 = 123;
$a->var2 = 987;
Is there a more better way to do this, since at times there can be even 20 such variables inside the class. I am just looking for compacting the code, and a constructor is not being used for this class. The values for variables are not static (that is why no static keyword is used)
I am looking more for this kind of assignment we do for arrays:
$arr = array(
'var1' => 123,
'var2' => 987
);
Furthermore, how will I go around avoiding NullPointerException (which sometimes comes up when I try to send this object to a .Net enabled web service) - This is optional though, as I can atlst relate this to null variables inside the class, but not exactly sure how to overcome this error.
Well the constructor could accept an array of parameters containing the values for these class variables which you set to the class variables inside the constructor. To avoid a null, just assign every class variable you declare to some basic value such as empty string, any value that you don't care about.
Is there a better way to assign values to properties of a class? Well, there are multiple ways, but one is not necessarily better than the other, and they are all going to involve a similar amount of code to accomplish.
Can't answer your .NET question; I'm not familiar with it.