Jump to content

Decalring Vars / Default Values


punk_runner

Recommended Posts

What is the difference between declaring values at the top of a class versus in between the parenthesis of a method?

 

Class MyClass {

      private $table = 0;
      public   $field = "name";

      // etc...

 

 


Class MyClass {

      private $_table;
      public $field;

      public function myFunction($_table=0, $field="name") {}

Link to comment
https://forums.phpfreaks.com/topic/224754-decalring-vars-default-values/
Share on other sites

Scope

 

*** oops sorry about that didn't mean to submit just that...here's the rest

 

When you declare the variables at the top of the class you are preparing them to be used anywhere in the class.

 

In the function declaration you are setting them as if they were local variables being passed for that function.

 

 

Class MyClass {

      private $_table = 1;
      public $field = "name";

      public function myFunction($_table=0) {
        echo $_table; // 0
        echo $this->_table; // 1
        echo $field; //
        echo $this->field; // name
      }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.