php - Initialize class in the same file -


this might seem stupid question but, practice initialize class or call 1 of it's functions in same file? mean this:

<?php class myclass {     public function __construct($param) {      }     public static function myfunction() {      } }  $obj = new myclass('hello'); myclass::myfunction(); ?> 

this works jut fine, i'm curios if practice, functionality , accessibility point of view.

no, it's not practice. the psr has codified in third point:

files should either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) should not both.

creating variables, class instances , calling code "side effect". cause problems if include file several times different places, or implicitly creating variable in current scope without way avoid that.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -