Is there any difference between these methods of declaring and setting a public class variable? Is there any reason why you would choose one over the other?
Method 1
class example {
public $myArray;
function __construct() {
$this->myArray = array(1, 2, 3);
}
function showVar() {
print_r( $this->myArray );
}
}
Method 2
class example {
public $myArray = array(1, 2, 3);
function showVar() {
print_r( $this->myArray );
}
}
In first case the code will be evaluated each time you create new class instance.
In second case - it will be evaluated just once when class was parsed.
That's all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.