Hello, Recently i've picked up php and I'm stuck. I'm following some net.tutsplus tutorials by Jeffrey Way on PDO database acces and I'm trying to incorperate this into a class without luck.
<?phpclass Database {function __construct(){ try { $conn = new PDO('mysql:host=localhost;dbname=blog', 'root', ''); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $error) { echo 'ERROR: ' . $error->getMessage(); }}function get($conn) { $stmt = $conn->prepare('SELECT * FROM users'); $stmt->execute(); while($row = $stmt->fetch()) { print_r($row); }}}$database = new Database;
Where I'm getting stuck is passing the $conn attribute from the construct method to my get method. After including this class into my index file I'm trying to call my class get method by:
<?php $database->get(); ?>
But I have no idea what to pass as argument. the $conn property holds the Data Object but I can't simply use $conn as argument because I will get an 'undefined variable' message wich is logically since it's in the constructor. What can I do to make this work, how can i pass the $conn variable from my constructor to my get method?