Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protected init #265

Merged
merged 5 commits into from
Aug 26, 2020
Merged

Protected init #265

merged 5 commits into from
Aug 26, 2020

Conversation

mvorisek
Copy link
Member

@mvorisek mvorisek commented Jul 28, 2020

Documented also to be not called directly, but quite large breaking change

How to migrate your code?

replace public function init() with protected function init()
and ->init() with ->invokeInit()

@georgehristov
Copy link
Collaborator

It is called by the container object:

$item->init();

$obj->init();

@mvorisek
Copy link
Member Author

mvorisek commented Jul 28, 2020

It is called by the container object:

yes, and also in ui and data... I prepared fixes

@mvorisek
Copy link
Member Author

init() should definitely be not public

sometimes (at least in ui) we need before/after init - now this can be done easily by overriding invokeInit()

@georgehristov @DarkSide666 wdyt?

@mvorisek mvorisek marked this pull request as ready for review August 26, 2020 09:24
/**
* Do not call directly.
*/
public function invokeInit(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of protecting init and then exposing it with another method?

Copy link
Member Author

@mvorisek mvorisek Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc9fe4a

there are a lot of situations when you need before/after init method - I was thinking about hooks, but this is fine as you can override invokeInit() - this is needed when init() itself is extended, which is very ofter in ui

and by making init() protected we can ensure it is (without hacking) not called directly

Copy link
Collaborator

@georgehristov georgehristov Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with that. Setting View::$model for instance needs to be done afterInit and not during init.
To follow common naming paterns (as in atk4/data) maybe we can use init, doInit (instead of invokeInit, init)?
So you keep init public but we move the actual routine under protected doInit. It will be no BC break then I believe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow common naming paterns (as in atk4/data) maybe we can use init, doInit (instead of invokeInit, init)?

doInit is horrible

there are two uses and they do not invoke!, they process the operation (delete) after beforeDel and before afterDel:
image

So you keep init public but we move the actual routine under protected doInit. It will be no BC break then I believe

Impossible, the init method must be protected and not called directly :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking about the atk4/data#690 where the actual operations are performed in doXx methods.

Impossible, the init method must be protected and not called directly :)

But we call directly the invokeInit? The matter is only of naming, mostly for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doXxx there is fine in Sql sense (ie. do/send query)

here we keep init as standard method to implement

But we call directly the invokeInit?

I understand your point - there are some uses, for easy migration, I propose to keep "some" method and keep it public, in optimal world, this method should never be called by used.

public function invokeInit(): void
{
// make sure init() method is never declared as public
$reflMethod = new \ReflectionMethod($this, 'init');
Copy link
Member Author

@mvorisek mvorisek Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: checked for every instance/call, but it is fast: +0.5% of total testing time for atk4/ui where used heavily

@DarkSide666
Copy link
Member

In my opinion this is huge breaking change and almost without any advantage.
init() now will be protected - so what? What's the advantage?
Also all this reflection magic just adds complexity of code and ... for what?

@georgehristov
Copy link
Collaborator

georgehristov commented Aug 26, 2020

The only thing that needs to be adressed is the afterInit functionality which we need to introduce.

For instane in UI View class we call View::setModel within the init method which is not correct and this method should be called after init completed. Call to setModel is at the end of View::init but all classes that extend it add more statements.
So this does not work as expected for Crud for example as routine in setModel expects some properties to be initiated in init but they are not as setModel is called at the end of View initiation which is the beginning of initiation of Crud.

@mvorisek
Copy link
Member Author

@georgehristov let's go on Discord and discuss the final version, I need this to be merged asap

Copy link
Member

@DarkSide666 DarkSide666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I agree :)

@mvorisek mvorisek merged commit 761c979 into develop Aug 26, 2020
@mvorisek mvorisek deleted the protected_init branch August 26, 2020 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants