简体   繁体   中英

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);
                        }
            }
?>

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

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);
    }
}
?>

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"
    }
}

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 (aka "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM