-2

When I try to execute this code from inherited class I got this error Using $this when not in object context here my code

abstract class Connection {

    private static $host;
    private static $username;
    private static $password;
    private static $database;

    public function __construct() {
        self::$host = 'localhost'; // database server address
        self::$username = 'root'; //database server username;
        self::$password = ''; //database server password;
        self::$database = 'oms'; //database name
    }

    private function connect() {
        return new PDO('mysql:host=' . Connection::$host . ';dbname=' . Connection::$database . '', Connection::$username, Connection::$password);
    }

    protected function execute($sql) {
        $this->connect();
        return $this->connect()->query($sql);
    }

}

what are the reason for that? i don't use any static method in Connection class. SoWhy do give this error?

4
  • 7
    Can we see the rest of your code? Commented Feb 8, 2015 at 10:20
  • 1
    You seem to mix static with non static methods. Make a decision on whether it's a static class or not. If it's a static class (like you seem to be using it) you need to change $this->connect to self::connect for example. Commented Feb 9, 2015 at 5:35
  • 1
    Please show us the exact error message. Also show us your full code! Commented Feb 14, 2015 at 22:33
  • ^^ Not the full code, but please post part of the inheriting class whose method you are trying to call. It is likely a static method called as ChildClass::something_using_connect() which is not valid. Commented Feb 14, 2015 at 22:52

1 Answer 1

0

This is probably what you want:

abstract class Connection {

    protected static $host;
    protected static $username;
    protected static $password;
    protected static $database;
    protected static $connection;

    public static function load() {
        self::$host = 'localhost'; // database server address
        self::$username = 'root'; //database server username;
        self::$password = ''; //database server password;
        self::$database = 'oms'; //database name
    }

    protected static function connect() {
        // Only create the connection once
        if (!self::$connection) {
            self::$connection = new PDO('mysql:host=' . self::$host . ';dbname=' . self::$database . '', self::$username, self::$password);
        }
        return self::$connection;
    }

    public static function execute($sql) {
        return self::$connect()->query($sql);
    }

}

// Because we can't use an abstract class
class ConcreteConnection extends Connection {}

// Execute a SQL call
ConcreteConnection::execute("SELECT * FROM `table`");

But then what you'll have is a Singleton Pattern which is a pain for testing and changing later. I'd recommend you make it like this:

abstract class Connection {

    protected $host;
    protected $username;
    protected $password;
    protected $database;
    protected $connection;

    public function _construct($host, $username, $password, $database) {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
    }

    protected function connect() {
        if (!$this->connection) {
            $this->connection = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database . '', $this->username, $this->password);
        }
        return $this->connection;
    }

    public function execute($sql) {
        return $this->connect()->query($sql);
    }

}

// Because we can't use an abstract class
class ConcreteConnection extends Connection {}

// Inject our parameters into our class
$connection = new ConcreteConnection('host', 'username', 'password', 'database');
// Execute a SQL call
$connection->execute("SELECT * FROM `table`");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.