Monday 15 July 2013

oop - Avoiding use of a global database handler variable in PHP, to someone who is new to object-oriented PHP? -


First of all: This question is a duplicate not is. My previous question on the subject (of all things) was closed as a duplicate. It was not useful at least in the bit - I did not understand anything. I'm totally new to OOP - I need an answer according to my level of understanding - I had no clue about what the issues were being asked about the answers to the associated questions. Just because the question appears can be similar, can not be a questioner - I have specifically requested in my previous question that any kind of understanding has an 'understanding' on my level ' answer. So, I ask again (and again, if I want to). Here goes:


I am still completely new to PHP, and even new to Oriental Oriented PHP, who taught it only last week itself Have started, and I still understand some concepts like heritage and which class is the abstraction - some of them have been able to raise quite a bit because I am working with PDO and for some time my database is connected Do not help in handling other questions, but otherwise I am still a newbie in the OOPHP, then bear with me if my question seems a bit 'root'.

I have every initial file (init.php) that is required at the top of each of my webpages, except for other things, from a MySQL database using PDFs such as: < Pre> try {$ dbh = new PDO (insertion data here); } Hold (PDOException $ e) {echo $ e-> GetMessage (); }

So, as I think, on the execution of this code, $ dbh is now installed as a PDO object in the bottom of my init.php file There is a great need to add files to functions such as user.func.php , images.func.php , etc.

The problem is that whenever I need to ask the database from within my function, then before being able to handle it, let me write a code as $ dbh Need to declare the I: function myFunction {global $ dbh; // Here's the problem! {$ Stmt = $ dbh- & gt; Try (some SQL here); $ Stmt- & gt; Executed (); } Hold {// etc}}

Now, I do not understand this problem Why I have to declare globally - I understand this much, but how can I Avoid this. I have read some stack overflow answers that why not to avoid global, I just do not know how.

How do I go about removing global $ dbh from my code? Can I create a database handler class and every time I intimate it and pass my connection settings in the constructor mode instead? (Looks a little unnecessary).

In fact, it is not sure how to change the usage of my global variable, so in my understanding surely the help is appreciated! In a very broad way - you are asking here that there are specific principles that it would be useful to write code. Understand before cultivating. I know it sometimes seems attractive, but taking a step back to know some principles is really useful.

The answers you are connected to, explain directly dependency injection, but this may be a better place to start () especially this comment:

end In - I suggest that you will see some simple frameworks which handle some stuff - and learn from reading the source code.

A super simple liability injection container is Pimple:

A quick look at the introduction - it's not scary and help you get the concepts involved needed.

No comments:

Post a Comment