Is it allowed to implicitly assign instance variables to an instance?
That is, inside a method of a class that has no instance variables, can I just do this?
$this->foo = "foo";
$this->bar = "bar";
and later just call those again? Will PHP just create instance variables in this case?
Yes. PHP will simply create any member variables that are referenced but have not been declared. I just tested it with the following code:
When executed, this outputs:
Which proves that this works. I still recommend declaring all class member variables at the top of the class. It’s what the OO programmers maintaining your code will expect to see.
FYI: I ran my test with the following version of PHP: