static function error using PDO query

I'm learning oophp and I have got a little problem.

After 2 hours of search I need your help :(

My class:

<?php
namespace App;
use \PDO;

class Database {
 private $_pdo = null;
 private static $_connexion = null;

 // + my 4 mysql const

 public function__construct(){
  $this->pdo = new PDO ('mysql:dbname='.const.';host='.const.'', const, const);
}

 public static function getPdo(){
  if(is_null(self::$_connexion)){
   self::$_connexion = new Database();
  }
 }

 public static function select($test, $test2){
  self::getPdo();
  if($test == "*"){
   $req = self::$_pdo->query("SELECT * FROM $test2"); // line 29
  }
 }
}
$test = Database::select('*', 'users');

Error: (line29) Fatal Er: Uncaught Error: Access to undeclared static property: App\Database::$_pdo

I don't understand what I have to do, I've not found any similar issues in my research.

Thanks for help!

Edit (sorry): God I'm idiot... I removed static because I had this : Notice: Accessing static property App\Database::$_pdo as non static in line 15 (new PDO...)

The real error is : (when $_pdo is static):

Call to a member function query() on null in line 29

Solutions

You are trying to access $_pdo as a static variable even though it is not declared as such.

This is a hack, but it works. You can adapt it to your class

class Test {

    private $_pdo ;
    private static $self ;

    function __construct() {
        self::$self = $this ;
    }

    public static function select() {
        self::$self->_pdo = 'test' ;    
    }
}

$t = new Test() ;
$t::select() ;
Tags: PHP / Pdo

Similar questions

php static methods - what is the difference between "static function" and "public static function"?
As the title says i cannot seem to see what the difference is between a public static and just a plain old static.. ie and Both appear to be public, if they are what is the point in declaring public if it is by default?
Problem with SQL query when calling a static function from another static function in the same class
I'm new to PHP, but not new to programming. I have a strange problem. It's such a simple thing, and I have the feeling the solution is simple also, but I tried for hours without luck. I have a model class User which contains the following function: It works as expected everywhere, returning a User object given the right username. Except in the User...
Access static property through static and non-static methods?
I have a class and it has some static, some not static methods. It has a static property. I'm trying to access that property inside all of it's methods, I can't figure out the right syntax. What I have is this: Can anyone tell me the actual syntax for this one?
PHP - Strict Standards: Accessing static property as non static - Nothing is static?
I'm at a loss. I have gone over my two classes extensively for hours. Nothing is static, nothing is being statically referenced, yet I cannot get rid of this error. Class A's file (ClassA.php) Class B's file (B.php) I am not new to classes, however I've never had this issue before (without having at least a static variable or method or static refer...
Is there a difference between PDO::exec and PDO::query when using PDO::ATTR_PERSISTENT = true?
There seems to be a difference between PDO::exec and PDO::query when having PDO::ATTR_PERSISTENT = true. When using PDO::exec connections will not be reused and in the end resulting in a "to many connections" error by MySQL as they do not seem to be closed (or reused). See for example this small code snippet: The exec will leave "sleeping" MySQL qu...
Fatal error: Uncaught Error: Call to a member function query()/prepare() on null. No PDO function works. it always says Call to a member function
This is my Class Code **And This is my ** Fatal error: Uncaught Error: Call to a member function query() on null in D:\Laravel\Xamp_new\htdocs\mou\bikroy\bikroyplus\class\DatabaseFunctions.php:33 Stack trace: #0 D:\Laravel\Xamp_new\htdocs\mou\bikroy\bikroyplus\admin\pages\users.php(12): DatabaseFunctions->userIndex('SELECT * FROM m...') #1 {main} t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.