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

ToroPHP and dependency injection #90

Open
Ecaz opened this issue Sep 3, 2014 · 3 comments
Open

ToroPHP and dependency injection #90

Ecaz opened this issue Sep 3, 2014 · 3 comments

Comments

@Ecaz
Copy link

Ecaz commented Sep 3, 2014

Hello! I just picked up ToroPHP and I normally use DI when I do any kind of database interaction.

Rough example

$db = new PDO("mysql:host=myhost;dbname=myname", "username", "password");
$object = new Object($db);

class Object {
    private $db;

    function __construct($dbConn) {
        $this->db = $dbConn;
    }

    function get($string) {
        $sql = $this->db->prepare("statement GOES here");
    }
}

But ToroPHP doesn't allow you to do this, is there any way to get around this? I don't wanna use a global variable.

Toro::serve(array(
    "/" => "Object",
    "/get/:string" => "Object" //would be injected somewhere so you could get :string from the db.
));
@rmrhz
Copy link
Contributor

rmrhz commented Sep 4, 2014

Question, in what kind of implementation are you trying to achieve?

@Ecaz
Copy link
Author

Ecaz commented Sep 4, 2014

What do you mean?
The project I'm working on right now would get data from a database based on the get request.
So if they browse to /get/port/12345 they would get port info for user 12345. Except it's an API so they wouldn't browse to it

And I might add that I'm not a fan of singleton.

@VeeeneX
Copy link

VeeeneX commented Sep 4, 2014

This is how Toro serve handler:

   $result = call_user_func_array(array($handler_instance, $request_method), $regex_matches);

So if you change it to something like this:

   $result = call_user_func_array(array($handler_instance, $regex_matches[1]), array($regex_matches[2]));

And define it like /{controller}/{action}/{params}
This might work.

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

No branches or pull requests

3 participants