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

Proposal: Way to use global #99

Closed
rustamwin opened this issue Oct 26, 2019 · 7 comments
Closed

Proposal: Way to use global #99

rustamwin opened this issue Oct 26, 2019 · 7 comments

Comments

@rustamwin
Copy link
Member

What steps will reproduce the problem?

Getting container globally

What is the expected result?

this is useful for functions
e.g: Container::getInstance()

What do you get instead?

Additional info

Q A
Version 1.0.?
PHP version
Operating system
@samdark
Copy link
Member

samdark commented Oct 26, 2019

That would result in using it as service locator and that is exactly what we've tried to avoid.

@terabytesoftw
Copy link
Member

terabytesoftw commented Oct 29, 2019

Hi, why not use delegate with a reflection class instead of injecting the entire container.

Example:

<?php declare(strict_types=1);

$container = new League\Container\Container;

// register the reflection container as a delegate to enable auto wiring
$container->delegate(
    new League\Container\ReflectionContainer
);

$foo = $container->get(Acme\Foo::class);

var_dump($foo instanceof Acme\Foo);           // true
var_dump($foo->bar instanceof Acme\Bar);      // true
var_dump($foo->baz instanceof Acme\Baz);      // true
var_dump($foo->bar->bam instanceof Acme\Bam); // true

@samdark
Copy link
Member

samdark commented Oct 29, 2019

@rustamwin I think it's better to get container instance to where functions are declared and pass it there via use ($container). Is it possible?

@rustamwin
Copy link
Member Author

@samdark I mean something else, global functions are declared here. I want declare new function something like this:

if (!function_exists('alert_success')) {
   function alert_success($message, $remove = false) {
       Container::getInstance()->get(FlashInterface)->add('success', $message, $remove)
   }
}

samdark added a commit to yiisoft/demo that referenced this issue Oct 30, 2019
@samdark
Copy link
Member

samdark commented Oct 30, 2019

Here you go: yiisoft/demo@af88e0b

@samdark samdark closed this as completed Oct 30, 2019
@rustamwin
Copy link
Member Author

Is this a temporary solution?

@samdark
Copy link
Member

samdark commented Oct 30, 2019

No, why? I want to make accessing container globally (except maybe these functions) a hard enough task so people won't abuse it without thinking.

samdark pushed a commit to yiisoft/demo that referenced this issue Nov 19, 2019
* Replaced the variable with the getter method. (#24)

* Remove unused views

* Update meta files

* Tune config

* Allow accessing container instance in globals.php

See yiisoft/di#99

* ContectController fix mailer params (#28)

* Added routes names and replaced static URL to dynamic (#29)

* Use `auth` package (#30)

* Config fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants