Jump to content

Making a class for PDO connection [Beginner]


legacy800
 Share

Recommended Posts

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?

Link to comment
Share on other sites

you should make $conn a private member/field of the Database class.http://www.php.net/manual/en/language.oop5.properties.php

Edited by thescientist
Link to comment
Share on other sites

which part? Did you see some of their examples?

class MyClass{	private $foo = FALSE; 	public function __construct()	{		$this->foo = TRUE; 		echo($this->foo);	}} $bar = new MyClass();

One important part in the description was this

Within class methods the properties, constants, and methods may be accessed by using the form $this->property (where property is the name of the property;
First thing I mentioned was making a private member (or property), like so in their example
class MyClass{  private $foo;  //your $conn variable}

next thing would be to use your constructor to set a value to that member, as opposed to a variable only defined in the constructor, which you were doing

class MyClass{  private $foo;  //your $conn variable    public function __construct(){	   $this->foo = TRUE;  //do your $conn PDO assignment stuff here   }}

using the $this->someProperty syntax, you have access to that variable in all methods of your class (like in the constructor). This is how you can reference $conn in your get method.

Edited by thescientist
Link to comment
Share on other sites

Ok it's pretty clear to me now, if you want to use properties in multiple classes you need to declare them outside the methods. Thanks for the help, I really appreciate it! :D
Sort of. The important part is that you declare it at the top of the current class, at a minimum. The way you define its visibility (public, private, protected) determines whether or not it can be used outside of the class, and to what extent.http://www.php.net/manual/en/language.oop5.visibility.php
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...