0

I'm building a small chat page so me and my friends can have some fun. I want to save the messages, so on possible connection lost we can see the old messages, so I decided to insert them into a database using PDO, but I receive an error:

Call to a member function prepare() on null

I guess that's because I can't use the PDO that way (in a class), but I'm not very familiar with classes:

<?php
$DBH = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '');
$DBH->exec('SET NAMES utf8');

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

require_once(dirname(__FILE__) . '/vendor/autoload.php');

class Socket implements MessageComponentInterface {
    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $connection) {
        $this->clients->attach($connection);

        print('Connection: ' . $connection->resourceId . "\n");
    }

    public function onMessage(ConnectionInterface $sender, $message) {
        $STH = $DBH->prepare("INSERT INTO chats (sender, message) VALUES (:sender, :message)");
        $STH->execute(array(
            'sender' => $sender->resourceId,
            'message' => $message,
        ));

        foreach($this->clients as $client) {
            $client->send($sender->resourceId . ': ' . $message . "\n");
        }
    }

    public function onClose(ConnectionInterface $connection) {
        //
    }

    public function onError(ConnectionInterface $connection, \Exception $error) {
        //
    }
}

$server = IoServer::factory(new HttpServer(new WsServer(new Socket())), 8080);

$server->run();
?>
  • 1
    Does this answer your question? [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Dharman Jan 10 '20 at 09:25
  • Please enable error reporting both for PHP and PDO. – Dharman Jan 10 '20 at 09:25
  • Try to initialize variable $DBH in class. – Simone Rossaini Jan 10 '20 at 09:27

1 Answers1

0

$DBH is outside of the scope of the class and then it can't be reached.

You can either use the global keyword to access a global variable (yuck) :

<?php
$foo = 'bar';

class Hello
{
    public function __construct()
    {
        global $foo;
        echo $foo . PHP_EOL;
    }
}

$world = new Hello(); // Outputs bar

Or use a field that you set in the constructor :

<?php
$foo = 'bar';

class World
{
    private $baz;

    public function __construct($var)
    {
        $this->baz = $var;
        echo $this->baz . PHP_EOL;
    }
}

$hello = new World($foo); // Outputs bar

Or eventually wrap everything concerning the DB connection in another class and extend the class needing DB informations to this class :

<?php
class Fooz // This being your DB class
{
    protected $foo = 'bar';
}

class Fuzz extends Fooz
{
    public function __construct()
    {
        echo $this->foo . PHP_EOL;
    }
}

$bob = new Fuzz();  // Outputs bar
Cid
  • 14,968
  • 4
  • 30
  • 45