How Do I define properties for a class in php?

I've received Mixed responses on this depending what walk-through I read,

I've defined a class with 2 functions.

I want both functions to have access to the DB credentials

Currently, this code does not work unless I Copy and paste the variables into each function.

What am I doing wrong here?

<?php
class database  {
function connect()  {
var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
                    }

function disconnect()   {
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
    mysql_close($con);
                        }
            }
?>

Answers


This block:

var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;

Should come before the function(), not inside it; but still inside the class definition.

And it's good form to add an explicit visibility; private to start with:

class database  {
    private $username="my_username";
    private $servername="localhost";
    // etc. etc.

Then, the functions refer to them as:

$this->username;
$this->con;
etc.

Ideally you will want to have those credentials to be passed in by the constructor:

private $servername;
private $database;
private $username;
private $password;
private $con;

function __construct($host, $user, $password, $dbname)
{
    $this->servername = $host;
    $this->username = $user;
    $this->password = $password;
    $this->database = $dbname;
}

Even more ideally, learn about PDO


Code styling aside, you need to define your variables outside the class methods but still inside the class. Something like:

class database {
    var $username = "my_username";
    // etc.

    function connect() {
        // connect code
        // $this->username == "my_username"
    }
}

To access object property you need to use $this->property_name

 $this->con = mysql_connect($this->servername,$this->username,$this->password);

Class code would go like this:

<?php
class database  {
    var $username="my_username";
    var $servername="localhost";
    var $database="my_DB";
    var $password="An_Awesome_Password";
    var $con;

    function connect()  {

        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
    }

    function disconnect()   {
        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
        mysql_close($this->con);
    }
}
?>

you have to use the variables outside the function and use the construct and destruct functions, they will ensure you the connection will be open and closed properly

class database  {
    private $username = 'username';
    private $servername = "localhost";
    private $database = "my_DB";
    private $password = "An_Awesome_Password";
    private $conId;

   public function __construct(){
        $con = mysql_connect($this->servername, $this->username, $this->password);
        $this->conId = $con;
        //..........
    }


    public function __destruct(){
        mysql_close($this->conId);
    }
}

It's good practice to assign class properties and methods their scope, which can be "public", "protected", or "private", instead of using "var". Also, class properties are assigned within the class but outside of any function (a.k.a. "method"). Here's your class refactored:

class database  {
    private $username="my_username";
    private $servername="localhost";
    private $database="my_DB";
    private $password="An_Awesome_Password";
    private $con;

    public function connect()  {
        if (!$this->con) {
            $this->con = mysql_connect(
                    $this->servername, $this->username, $this->password);

            if (!$this->con)  {
                die('Could not connect: ' . mysql_error());
            }
        }
    }

    public function disconnect()   {
        if ($this->con)  {
            mysql_close($this->con);
        }
    }
}

Variables are only available to be called within the scope they are created in.

If you create a variable in a function it can only be used in that function, but the higher you define it on the ladder the more it is available to.

Global Public Private Function


Need Your Help

Using jaxb marshal xmlns attribute missing from xml output

xml xsd jaxb xml-namespaces

I have some jaxb objects that are generated from an xsd I created. Previously when I marshalled the content, the xml output looked like this:

Path with first degree relations in neo4j

java neo4j

I've a huge (20*10^6 nodes) network, and I would like to return a path from node a to node b, let's say of length 4. My problem is that I also need to display the children of the traversed nodes.