joomla-framework icon indicating copy to clipboard operation
joomla-framework copied to clipboard

Refactor usage of magic getters in apis ?

Open florianv opened this issue 11 years ago • 4 comments

The magic getters inside a package can be handy but you can do this :

<?php
$github->issues->issues->issues....

which is not really nice to allow.

I think, ideally it should be structured this way

<?php

$issueManager = new IssueManager($options, $client);

// Finding an issue
$issue = $issueManager->findById(8);

// Creating an issue
$issue = new Issue();
$issue->setTitle('hello');
$issueManager->save($issue);

Each issue is a plain object with setters and getters and the manager acting as Data access object. Ideally there should be an other layer so you can change the http client.

florianv avatar Dec 03 '13 00:12 florianv

I like that approach, it keeps separation of concerns, in that the object is not responsible for saving, as it would be in an ActiveRecord pattern. This would be something to consider for v2, as it would be a pretty big BC break.

dongilbert avatar Dec 03 '13 00:12 dongilbert

Yes it would be an "Object Http Mapper"

florianv avatar Dec 03 '13 00:12 florianv

I think things like issues can be much simpler data objects. I don't see the need for getters and setters because we don't really have any control over the data structure. In other words, $issue->title = "Foo" is less overhead than $issue->setTitle('foo'). Then you could certainly do something like $issuesService->update($issue);.

eddieajau avatar Dec 03 '13 00:12 eddieajau

Yes. Setters are only useful for typehinted params, but then it's a question of consistency, i.e if I use a setter once, maybe I should use them everywhere.

For example if we have somewhere in the package :

$issue->setRepo(Repository $repo) it would be odd to use later $issue->title = 'hello';

florianv avatar Dec 03 '13 01:12 florianv