A
A
Alexander Gomzyakov2012-02-27 15:16:03
Microcontrollers
Alexander Gomzyakov, 2012-02-27 15:16:03

How justified is the solution with non-class controllers?

In many popular frameworks, such as Zend, Symfony or Yii, and in general in PHP applications that I have come across, the controller is an instance of some base class. On the other hand, there are no less rare solutions where controllers work with environment objects, in addition to which only models are wrapped in classes.
I understand that the system looks slimmer when the controllers are also objects, but at the same time, I do not see anything wrong with the global visibility of several objects in the environment (router, logger, etc.). The opinion that controllers that are not objects are faster or eat less memory also does not contradict my personal intuition.
I would like to hear the opinion of comrades who are more experienced in programming, to understand how justified the solution with non-class controllers is, what are the pros and cons of such an approach?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Anatoly, 2012-02-27
@taliban

There is nothing wrong with this, as well as an increase in performance. creating a class is a trifle action, it is much slower to find and load a file (and this is done in both cases). They do more with classes in frameworks because of the uniformity (one element - one class and flexibility - oop functionality). If you do not need it, then nothing at all prevents you from using controllers as classes.
But "flexibility - functionality oops" - This is usually needed.

R
rPman, 2012-02-27
@rPman

So what? In my code there are 'terrible' constructions of the form:

function base(){global $base;return $base;}

What's the difference how to access the factor? this is purely syntactic garbage ... you just can’t access an instance of a class without global (of course, you can ideologically beautifully shove it into all objects ... but is it worth it), and static fields of a class have restrictions (there are methods ... but they also have to access global objects )

I
Ivan, 2012-02-27
@dohlik

First of all, it occurred to me - what will you do with inheritance (more precisely, its absence)? It is very convenient to make several basic controllers in which to implement the foundation (checks, initialization of properties, loading configs, etc.). Separate a separate script for this, and include it in each controller?
Of course, if the project is not so complicated, then the question will not arise)))

V
Vladimir Chernyshev, 2012-02-27
@VolCh

In symfony, it seems that the controller does not have to be a class at all. Rather, it cannot be a class in principle. It can be a class method, it can be a function, in general, any callable.
If at all we mean the code of the global namespace without any wrapper (practically "spaghetti" code, only with the separation of M and V), then it is categorically against - it greatly complicates any sharing, because the variables of any controller become variables of the global namespace.

E
egorinsk, 2012-02-28
@egorinsk

What is the advantage of $GLOBALS['router']->… over App::getRouter()->....?
The second option: 1) allows you to replace the router in 1 place with another one, allows you to write neat code without global variables, more beautiful. The first option has no advantages.
Regarding memory consumption, you will have memory consumption if there are many classes and many instances are created, and if you have 1 static App class, there is almost no overhead.
Object controllers support inheritance, which is handy. But in general, a good framework allows you to write code like:
require_once 'framework-bootstrap.php';
if (!App::TableGateway('People')->add(array('name' => 'John', 'surname' => 'Johanson'), $errors)) {
die( "Error");
App::templateEngine()->render('success.html');
}
And leaves the author the choice to use objects or not.
As for PHP libraries and frameworks, don't look at them. Among the well-known libraries, there are many absolutely inadequate things that, for example, are ported from Java, without realizing that the price of an object / class in PHP / JAva is different, the performance is different, the work cycle is different. The result is monsters like Zend or an ugly port of Hibernate.
In general, I have a very bad attitude towards Open-source products in PHP, they are written randomly, thoughtlessly, and I do not advise taking an example from them, but thinking with your own head.
You don't need a logger - write either App::log(string) or file_put_contents('', '', FILE_APPEND) right away - no one will read these logs anyway.

D
deilux, 2012-02-28
@deilux

An explicitly designated controller is, as already mentioned, the ability to use inheritance and put some common things for the entire application (or section of the site) into the base class.
And this is the only way to ensure competent reuse of the code (theoretical blabla) and the ability to test this code at least somehow.
Access to global things drastically narrows the applicability of the written code and the room for maneuver. If you do it, do it wisely!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question