06.
More Fun with use Statements
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.
12 Comments
Hi there I am getting a Fatal Error saying "Uncaught Error: Class 'Service\PDO' not found in C:\Users\PatrickLye\github\PHP\oiii\lib\Service\Container.php on line 28"
line 28 has this on it
"$this->pdo = new PDO(
$this->configuration['db_dsn'],
$this->configuration['db_user'],
$this->configuration['db_pass']
);"
Any suggestions?
Hey Patrick L.
Seems like you forgot to add the PDO class import. Can you double-check the imports at the top of your file?
Cheers!
The top part of my first comment is missing this:
namespace Service;
class Container
{
private $configuration;
private $pdo;
private $shipLoader;
private $battleManager;
private $shipStorage;
public function __construct(array $configuration)
{
not enteriely sure why it got removed
Okay I haven't a clue how I fixed it, but I did my code went from this:
configuration = $configuration;
}
/**
* @return PDO
*/
public function getPDO()
{
if ($this->pdo === null) {
$this->pdo = new PDO(
$this->configuration['db_dsn'],
$this->configuration['db_user'],
$this->configuration['db_pass']
);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $this->pdo;
}
/**
* @return ShipLoader
*/
public function getShipLoader()
{
if ($this->shipLoader === null) {
$this->shipLoader = new ShipLoader($this->getShipStorage());
}
return $this->shipLoader;
}
public function getShipStorage()
{
if ($this->shipStorage === null) {
//$this->shipStorage = new PdoShipStorage($this->getPDO());
$this->shipStorage = new JsonFileShipStorage(__DIR__.'/../../resources/ships.json');
}
return $this->shipStorage;
}
/**
* @return BattleManager
*/
public function getBattleManager()
{
if ($this->battleManager === null) {
$this->battleManager = new BattleManager();
}
return $this->battleManager;
}
}
to this:
configuration = $configuration;
}
/**
* @return PDO
*/
public function getPDO()
{
if ($this->pdo === null) {
$this->pdo = new PDO(
$this->configuration['db_dsn'],
$this->configuration['db_user'],
$this->configuration['db_pass']
);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $this->pdo;
}
/**
* @return ShipLoader
*/
public function getShipLoader()
{
if ($this->shipLoader === null) {
$this->shipLoader = new ShipLoader($this->getShipStorage());
}
return $this->shipLoader;
}
public function getShipStorage()
{
if ($this->shipStorage === null) {
//$this->shipStorage = new PdoShipStorage($this->getPDO());
$this->shipStorage = new JsonFileShipStorage(__DIR__.'/../../resources/ships.json');
}
return $this->shipStorage;
}
/**
* @return BattleManager
*/
public function getBattleManager()
{
if ($this->battleManager === null) {
$this->battleManager = new BattleManager();
}
return $this->battleManager;
}
}
I'm getting errors around mcrypt_module_self_test()::TYPE_NO_JEDI in BattleManager.php. They've only just started, I'm on PHP v 7.2 and this function is deprecated. I'm not really sure as to why it's being used, could someone please enlighten me? Many thanks . Si.
Hey Si Y.
This tutorial was coded on PHP5 that's why you will probably find some deprecated functions being used (but it should be nothing to worry about). Can you show me what error you got?
Cheers!
I can't check the challenge, no response from the server
Yo Hakim Ch! Try it again - I saw that (once) the server you were using shutdown (it does that for security after about 20 minutes). Or there may have been some other connection problem. Anyways, sorry about any issues - I can see the problem in the logs, but it looks temporary.
Cheers!
Super!!! challenge done! your the best :D
What is the purpose of the coloring of the files in the document tree on the left side? It changes during the process of adding namespaces and in my own code I saw at some point also red coloring for instance.
Yo Max!
You're playing close attention :). PHPstorm highlights the files in different colors based on their storage status in git. Behind the scenes. this project is being stored in git, and between the chapters, I actually commit all of my changes. You'll notice that the tree at the end of one chapter has different colors than the start of the next chapter because of this (nobody has ever noticed that before!). I believe blue is a modified file and red is a new file.
Cheers!
And you are right again... :D Actually a quite useful feature, if you know about it :) Thanks!
"Houston: no signs of life"
Start the conversation!