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
Post a Comment