cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Array Oriented Programming

Open e-oz opened this issue 13 years ago • 22 comments

In examples I see a lot of Array Oriented Programming anti-pattern cases:

array( "prefix" => 'cache', "host" => "localhost", "port" => "11211" )

Why not $obj->setPrefix('cache')->setHost('localhost')->setPort('11211') ? In array-style arguments we always should keep in memory whole set of available keys (even trees of keys), which keys are optional and which are required - all in memory, without tips from IDE. It's a bad practice.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

e-oz avatar Mar 07 '13 11:03 e-oz

I agree too with this. Many features lost from users because of unknowing of possible variables to use. I would be very very cool, if were like jamm posted. All methods then could be seen in API indice =))

hesalx avatar Mar 07 '13 16:03 hesalx

The next few weeks we will revisit the documentation and tests heavily to ensure that all features are clearly marked with examples for developers.

@Eugene, good suggestion. We will see how we can incorporate this. If it is not feasible now (we need to ensure that the API remains the same), then we will make clear on what the default values each component/function needs.

niden avatar Mar 07 '13 21:03 niden

@jamm

Array-style arguments everytime good because easy way to bind meta programming or configuration based patterns. array-style shouldn't be left. Getter and setters should be added according to need on application logic. setPrefix, setHost, setPort unnecessary because these setters not will be require in application logic.

Did you ever need to use setPort method in application logic? Answer: NOOO

There are already some setters which required in application logic like $response->setHeader

tugrul avatar Mar 09 '13 19:03 tugrul

@tugrul can you say something more detailed than "NOOO"? Maybe some counter-arguments about keeping set of arguments in memory? about required and optional arguments? Try to search and you will find a lot of examples with setters.

NikitaT avatar Mar 09 '13 20:03 NikitaT

@NikitaT

Did you ever need to change mysql port in somewhere application logic like following?

switch($this->request->getPost('things') {
    case 'foo':
        $this->connection->setPort(1234);
        break;
    case 'bar':
        $this->connection->setPort(4321);
        break;
    default:
        $this->connection->setPort(9876);
}
$this->connection->connect();

Be a little realistic. The theory is not everything. Why don't you use Java instead of PHP? The connection constructs on bootstrap everytime.

tugrul avatar Mar 09 '13 20:03 tugrul

Array can populate from configuration file or configuration record somewhere in the SaaS.

tugrul avatar Mar 09 '13 20:03 tugrul

@tugrul is right.

    $di['db'] = function() use ($config) {
        return new Db\Adapter\Pdo\Mysql((array)$config->database->app);
    };

Imagine this without array-argument opportunity =)

I think both of ways should exist. Anyway, the current structure of framework wouldn't be change abruptly because of compatibility. But there is always something new can be added.

hesalx avatar Mar 09 '13 20:03 hesalx

I agree with @tugrul. It makes little sense in setters, where it is a global setting/configuration of the application. But in the logic of the application setters are really useful.

nikolasha avatar Mar 10 '13 11:03 nikolasha

Perhaps it makes sense to make setters and getters for standard services in class Phalcon\DI. Or better to make a class extension Phalcon\Mvc\DI and there add the setters / getters the standard services.

nikolasha avatar Mar 10 '13 12:03 nikolasha

@Eavu it's not so difficult to imagine actually, look at example in my first post here. @nikolasha files of settings and arrays of arguments is absolutely different things. In file of settings you can see all necessary keys and just change their values manually. You don't have to remember keys or each time look for the documentation, read documentation (which can be not comprehensive enough).

Array settings, global variables and singletons - just poor legacy of old frameworks, like CI.

e-oz avatar Mar 10 '13 15:03 e-oz

All is relatively. One features are useful for one cases, other — for another. I my example I mean that you can add of delete property without editing .php but .cfg only. With setters way you will have to add/delete new setter to the .php file and the the .cfg file. This reduces the sense of config file a bit... The "power" of array is it is a single variable, you could change only source the array came from without change the place where the array is used.

I think that the right way is to have many means to be each the most useful in its special case. For example (first method doesn't exists, just imagination):

$this->dispatcher->forward('home', 'index');
// instead of 
$this->dispatcher->forward([
    'controller' => 'index',
    'action'     => 'home',
]);

but

$this->dispatcher->forward($dynamically_changed_array);
//instead of
$this->dispatcher->forward($array['action'], $array['controller'], $array['params'], $array['namespace']);

P.S.: Why does singletons are poor legacy? Them just provide needed functionality. For example Request class has to be singleton by definition. And not so many classes are singletons. View, for example, which can be cloned to separated one's object.

hesalx avatar Mar 10 '13 15:03 hesalx

@Eavu try to move all "dynamically changed" arrays to config-files and you'll see huge difference. About P.S.: your app can send much more than just one Request, so it shouldn't be singletons. http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ and many other in web.

e-oz avatar Mar 10 '13 15:03 e-oz

@jamm you think with hardcoding approach. We are mentioning to automation.

Can you easily made override config without array like following? Don't you write dozens of if conditions with isset/empty controls?

    $configBase = (array)$config->database->app;
    $configApp = $mongoDbResultOfHostnameRoute->database;
    $config = array_merge($configBase, $configApp);

    $di['db'] = function() use ($config) {
        return new Db\Adapter\Pdo\Mysql($config);
    }

tugrul avatar Mar 10 '13 15:03 tugrul

@jamm, send? For sending there is non-singleton Response http://docs.phalconphp.com/en/latest/api/Phalcon_Http_Response.html Request should be singleton because a php-app by definition represents the performance of a one single request. For each request there is created a separated app instance. Because it's how does php works at the moment. Exactly that's why there is concept "Web MVC", or passive MVC. You are talking about something like active (or "native") MVC. Singletons is a little bit off-topic topic there, so lets don't continue it. At this thread, at least.

hesalx avatar Mar 10 '13 15:03 hesalx

@Eavu I agree with all of the cfg logic. We can migrate our apps to new features of framework without change the app code, upgrade framework and add new parameters to cfg only :)

Arrays like semantic pipes among modules/systems.

tugrul avatar Mar 10 '13 16:03 tugrul

@tugrul I never think about hardcoding positively. In your example just change $config array to $Config object and you'll get setters/getters generated by IDE and full code completion in IDE and phpdocs for each option.

@Eavu php app can receive dozens of requests. And yes, I'm not interested in arguing about singletons, all is written enough about them :)

e-oz avatar Mar 10 '13 16:03 e-oz

@jamm it means dozens of different config entities. PHP is not strict language due to the nature of self.

Zend Framework looks like good for UML standards and has got beautiful design. I used Zend Framework before but I'm not stay satisfied because it congested with some extreme problems.

One of the problems is performance, other one of the problem is application abstraction.

I'm using Phalcon because more flexible than Zend Framework. Phalcon resolves common problems and leave to you other parts. You can assemble modules without congest using Phalcon because it not forces you to use narrow molds.

Do you like JSON? Why JSON is powerful? Why JSON objects is not instance of different entities?

tugrul avatar Mar 10 '13 18:03 tugrul

@niden it's still ok to add better stuff to API deprecating and then removing In next releases some parts of it. If you add another exhaust Pipe, spoiler and glue digital meters on top of analogue once, you still won't get top of the range Bentley out of Ford Fiesta.

roman-kulish avatar Mar 30 '13 11:03 roman-kulish

Symfony has an OptionsResolver component which I have found to be useful when using arrays in your constructor for configuration. It doesn't solve the issue with documentation and IDE issues though.

camspiers avatar Jun 10 '13 05:06 camspiers

I totally agree with @jamm . Objects with setter/getters make it won't require optional documentation to lookup what argements you can pass. It makes code less faulty, easier and faster to develop and migrating easier.

That PHP isn't strongtyped doesn't mean you can't use best practices....

RdeWilde avatar Oct 21 '13 14:10 RdeWilde