简体   繁体   中英

How to access a connection handle from one class in another class in php

I´m building a MVC application. My PDO connection is in /Controller/includes/Conexao and queries are in /model/DAO

As I need a prepared statement I need to use the connection handle like that:

$stmt = $db->prepare($query);

Problem is that $db is a variable in my class Conexão and I am not able to use it in DAO class. So how could I proceed?

Using old fashioned php code I could only call mysql_query but it is about to be discontinued so that´s not an option.

I would avoid using globals unless there is a VERY good reason for doing so. Look into dependency injection and singletons. There is a very nice light-weight container called Pimple that can be used for this purpose:

http://pimple.sensiolabs.org/

Another uglier solution would be using mysqli_query (only the old mysql is deprecated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM