Best method of defining a public class variable in PHP

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 );
  }

}

Answers


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.


Need Your Help

ScalaToolsSnapshots alternative resolver

scala sbt

Can anyone please help me with the alternative to ScalaToolsSnapshots resolver currently listed in sbt._?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.