PHP: Best practice regarding "public" visibility in OOP -


do add public beside methods , properties inside classes? or leave out?

1. option 1, without public:

<?php class simpleclass {     // property declaration     $var = 'a default value';      // method declaration     function displayvar() {         echo $this->var;     } } ?> 

2. option 2, public:

<?php class simpleclass {     // property declaration     public $var = 'a default value';      // method declaration     public function displayvar() {         echo $this->var;     } } ?> 

personally think adding public adds little more clarity code, though considered best practice?

best practice pick coding standard , follow (and put info somewhere in code).

i guess psr commonly used in php:

https://github.com/php-fig/fig-standards/tree/master/accepted

and according psr-2:

"visibility must declared on properties."

so second option way go.

you can check this:

http://www.phptherightway.com/


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -