3

I have

class Check
{
 public function __construct()
 {
 $this->db = new Database();
 }
 public function query()
 {
 $login = Session::get("login");
 $sth = $this->db->prepare('SELECT admin FROM accounts WHERE login=:login');
 $sth->execute(array(':login' => $login));
 $result = $sth->fetch(PDO::FETCH_NUM);
 return $result[0];
 }
 public static function admin()
 {
 echo self::query();
 }
}

I have Database class in another place with PDO connection.

class Database extends PDO
{
 public function __construct()
 {
 parent::__construct('mysql:host=localhost;dbname=name','root','pass');
 $this->query('SET NAMES utf8');
 }
}

So after Check::admin() code I get error:

Undefined property: View::$db

Why?

bukart
4,9162 gold badges23 silver badges40 bronze badges
asked Oct 27, 2012 at 13:02

2 Answers 2

6

You are using a static method, that wants to use a instance variable.

Your admin method calls the query method, and the query method is using the db instance variable. As your class is not instantiated, the db variable does not exists.

My suggestion would be to make the admin method non static and use your code like this:

$mycheck = new Check();
$mycheck->admin();

or, if you are on php 5.4 and want to stick with a oneliner:

(new Check())->admin();

update

note: Do not create the db class in the constructor, but inject it:

public function __construct(Database $db)
{
 $this->db = $db;
}
answered Oct 27, 2012 at 13:05

6 Comments

Thanks! But my task is it use a static method. I should do query in mysql and get result: user is admin or not.
Well if you want to access a instance variable over a static method, instantiate the class: $mycheck = new Check(); $mycheck::admin(); This is bad programming. If you are doing a course that teaches you this, get a refund.
I have self-education. Thanks for your opinion! I just want make possibility to call method like a static without instance. Less code, u know. But if u said that its bad programming I will change my code.
statics in classes are a fancy way to write procedural code. Better do it the OOP way. You will benefit a lot from that as you are moving on.
Yep. Im agree with u. But what if I need often to check user: he is admin or not? All time I should create a new instance?
|
1

Sorry this is not direct answer for your question but your code has some issues so take some time and examine this and ask if it's not clear for you.

<?php
class Check {
 protected $_db;
 public function __construct(Database $db) {
 $this->_db = $db;
 }
 public function query(ISession $sessionData) {
 //WHY IS THE SESSION STATIC?
 //$login = Session::get("login");
 $sth = $this->_db->Connection()->prepare('SELECT admin FROM accounts WHERE login=:login');
 $sth->execute(array(':login' => $sessionData->get("login")));
 $result = $sth->fetch(PDO::FETCH_NUM);
 return $result[0];
 }
 public function admin(ISession $sessionData) {
 // REALLY BAD TO ECHO HERE
 echo $this->query($sessionData);
 }
}
class Database {
 private $_name;
 private $_password;
 private $_connStr;
 private $_settings;
 private $_pdo;
 public function __construct($connstr, $name, $password, array $settings = array()) {
 $this->_name = $name;
 $this->_password = $password;
 $this->_connStr = $connstr;
 $this->_settings = $settings;
 }
 public function Connection() {
 if ($this->_pdo == NULL) {
 $this->_pdo = new PDO($this->_connStr, $this->_name, $this->_password);
 }
 return $this->_pdo;
 }
 /* other fancy methods */
 public function Close() {
 $this->_pdo = NULL;
 }
 public function __destruct() {
 $this->Close();
 }
}

And i don't see why you need a Check class for all this becouse if i were you i would create somethinf like this:

$currentSession = Session::GetCurrent();
$currentSession->User()->IsInRole('admin');

Note that the session is not static and if i would write a more complete solution i would avoid Session::GetCurrent() call becouse the current session would be a field in some class' instance (HttpApplication for example).

answered Oct 27, 2012 at 13:55

1 Comment

Session is static because I have another class Session ;) Echo in admin() I had for test. Im agree with u that its so bad :) Databse class I have in another file. There only PDO connection. And I manually change username, password and dbname of mysql. So it is why I didnt used a private variables diffirent. Thanks for ur help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.