I am making a PHP blog library. I am new to web development and only really programme in C++ or Python. So, I have probably done something really stupid, but this code doesn't work:

<?php
class Blog
{
    public $version = 1.0;
    public $mysql = NULL;

    public $servername = NULL;
    public $username = NULL;
    public $password = NULL;
    public $dbname = NULL;

    public function connectToDatabase($server, $user, $pass, $db) {
        $this->$servername = $server;
        $this->$username = $user;
        $this->$password = $pass;
        $this->$dbname = $db;
        $this->$mysql = mysql_connect($this->$servername, $this->$username, $this->$password);
        if (!$this->$mysql) {
            die('Error 500: Internal server error. :(<br /><br />Could not connect to ' . $this->$servername . ': ' . mysql_error());
            return false;
        }

I get the error: Fatal error: Cannot access empty property in /home/tom/public_html/Projects/PHPBlog/blog.php on line 13

I am making a PHP blog library. I am new to web development and only really programme in C++ or Python. So, I have probably done something really stupid, but this code doesn't work:

<?php
class Blog
{
    public $version = 1.0;
    public $mysql = NULL;

    public $servername = NULL;
    public $username = NULL;
    public $password = NULL;
    public $dbname = NULL;

    public function connectToDatabase($server, $user, $pass, $db) {
        $this->$servername = $server;
        $this->$username = $user;
        $this->$password = $pass;
        $this->$dbname = $db;
        $this->$mysql = mysql_connect($this->$servername, $this->$username, $this->$password);
        if (!$this->$mysql) {
            die('Error 500: Internal server error. :(<br /><br />Could not connect to ' . $this->$servername . ': ' . mysql_error());
            return false;
        }

I get the error: Fatal error: Cannot access empty property in /home/tom/public_html/Projects/PHPBlog/blog.php on line 13

try to define your server name as localhost

When referencing vars, you do not need a second $ :

$this->$servername = $server;

Would become

$this->servername = $server;

@tmash
The whole idea of Object Orientated programming is to make your code portable, not every server will have MySQL on localhost so defining this absolutely is not a good idea. The server address is passed to the script when the function is called.

When referencing vars, you do not need a second $ :

$this->$servername = $server;

Would become

$this->servername = $server;

@tmash
The whole idea of Object Orientated programming is to make your code portable, not every server will have MySQL on localhost so defining this absolutely is not a good idea. The server address is passed to the script when the function is called.

yes you are right i did not perfectly see that error:
$this->servername = $server;

Thanks, it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.