__construct uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies
of your class. This makes your code less dependent on global state and it
becomes generally more testable:
// BadclassRouter{publicfunctiongenerate($path){return$_SERVER['HOST'].$path;}}// BetterclassRouter{private$host;publicfunction__construct($host){$this->host=$host;}publicfunctiongenerate($path){return$this->host.$path;}}classController{publicfunctionmyAction(Request$request){// Instead of$page=isset($_GET['page'])?intval($_GET['page']):1;// Better (assuming you use the Symfony2 request)$page=$request->query->get('page',1);}}
Loading history...
16
{
17
$basePath = $_SERVER['DOCUMENT_ROOT'];
18
$path = $basePath . '/' . trim($path, '\/');
19
if (empty($path) || !(is_dir($path) || is_file($path)) || !is_writable($path)) {
20
throw new Exception('Путь не может быть пустым или нет доступа');
The return type of return new \RecursiveIte...ectoryIterator($path)); (RecursiveIteratorIterator) is incompatible with the return type declared by the interface Dumkaaa\BxOptimize\Finde...derInterface::findFiles of type array.
If you return a value from a function or method, it should be a sub-type of the
type that is given by the parent type f.e. an interface, or abstract method.
This is more formally defined by the
Lizkov substitution principle,
and guarantees that classes that depend on the parent type can use any instance
of a child type interchangably. This principle also belongs to the
SOLID principles
for object oriented design.
Our function my_function expects a Post object, and outputs the author
of the post. The base class Post returns a simple string and outputting a
simple string will work just fine. However, the child class BlogPost which
is a sub-type of Post instead decided to return an object, and is
therefore violating the SOLID principles. If a BlogPost were passed to
my_function, PHP would not complain, but ultimately fail when executing the
strtoupper call in its body.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: