09.
OO Best Practice: Centralizing Configuration
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
13 Comments
dont really get, what dependency injection is? its when you create another class and pass the arguments to its constractor function?
Hey Arturas!
Basically yes. But let me give you a *longer* explanation in case it helps :).
First, you decide that you want to create another class to hold some logic/code. That itself is not dependency injection - that is just you deciding that you want to organize your code a little bit :). But, once you have this other class - e.g. ShipLoader - and that classes needs some configuration (or it needs some other object, like a database connection object), you have a problem: how can you get access to that from inside ShipLoader? You only have two options: (1) Use a "global" keyword or some static variables (both are basically the same thing) or (2) force that configuration to be passed into your object - this can be done in a few ways, but use a constructor is the most common. The second (2) option is dependency injection. It's basically where you say "instead of using global variables, I'm going to setup my code so that anything I need is passed to me". Sometimes it's hard to explain because it's - in many ways - such an underwhelming concept: it's the way you're supposed to be coding all along.
Cheers!
So in other words it's passing values through __construct function ? And of course it's a must to pass values, otherwise class will not be accessible right ?
P.S. Sorry for terrible English.
Hey!
Exactly: dependency injection (roughly) means passing values (strings, integers, other objects) through the __construct() method. And you're correct: if you do not pass a value into a class, then you cannot access that value from inside the class (unless you use some global variables).
Technically, there is one other way to do dependency injection - but it is ultimately the same thing. This is by calling a set* function. For example, if I need to pass a dbDsn value into my class, I can create a __construct() method. Or, instead, I can create a setDbDsn($dbDsn) method, which sets the property and call this instead. In both cases, you are passing the dbDsn value into your class - these are just 2 different ways of doing it - they both have the same result. Which is better? Using __construct() is more common and a little easier in my opinion. But, it's interesting for our conversation :).
Cheers!
Interesting, but you forgot to mention updating the battle.php code too in this chapter.
Refresh! Still not broken! isn't true if you refresh the result page.
Hey Marc,
Thanks for this report. Actually, we only work with index.php in this chapter. So, we changed only index.php and then on video I see we refresh the index.php. For now we do not touch battle.php page, we will update it in further chapters.
Cheers!
My mistake !
I don't use video to follow lessons (I don't like the presenter's voice, it reminds me of automatic text readers).
I’m getting the following error:
«Catchable fatal error: Object of class PDO could not be converted to string in /lib/ShipLoader.php on line 79»
`
private function getPDO()
[79] return $this->$pdo;
`
I’m pretty sure I’ve got everything correct, so wondering if this might be a setting on my server config.
Hey Hanspln,
On line 79 you're calling property from variable, i.e. "return $this->$pdo;" but you should call the property explicitly: "return $this->pdo;".
Cheers!
I'm kind of lost as to what I'm doing wrong. I'm now getting a "connection refused" error. Thanks in advance:
<strong>ShipLoader.php</strong>
<strong>bootstrap.php</strong>
<strong>index.php</strong>
Hey Danny,
I bet your host is incorrect - you need to specify the port in different option. And better replace localhost with 127.0.0.1.
Try this:
mysql:host=127.0.0.1;dbname=oo_battle;port=8889Does it fix the problem?
Cheers!
I apologize once again. I just needed to restart my server(i.e. MAMP). Thanks!
Hey Danny,
Easy win! ;)
Cheers!
"Houston: no signs of life"
Start the conversation!