PHP Initialize class variables without constructor

I have the following class for example

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 better way to do this, as sometimes there can even be 20 such variables in a class. I'm just looking to compact code, and no constructor is used for this class. Values ​​for variables are not static (therefore, no static keyword is used)

I am looking more for the purpose we do for arrays:

$arr = array(
'var1' => 123,
'var2' => 987
);

Also, how do I get around avoiding a NullPointerException (which sometimes occurs when I try to send this object to a web service with .Net support enabled). This is optional, as I can relate this to null variables inside the class, but not exactly how to overcome this error.

+3
2

, , , . , , - , , , .

+1

? , , , , .

.NET; .

0

Source: https://habr.com/ru/post/1778413/


All Articles