Jump to content

Decalring Vars / Default Values


punk_runner
 Share

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
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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.