Basic class question on sending information to the class

I am taking a look at classes AGAIN and there is one major part that I still can not find the answer for - probably as I do not know what I am looking for!

All the examples I have found tend to use the variable contents in the class for example this one - only first few lines:


class createConnection
{
    var $host="localhost";
    var $username="user"; 
    Var $password="password";
    var $database="database";
    var $myconn;

// Called using: $connection = new createConnection();

Now this is OK but what if I want to use the same host, username and password on a different database.


$database='database_2';
$connection = new createConnection($database);

The above does not seem to work - I was expecting it to overwrite var $database=“database”;

You need a constructor method and preferably accessor methods (rather than access class properties directly) to extract property values.

<?php

class createConnection {

    protected $host = "localhost";
    protected $username = "user";
    protected $password = "password";
    protected $database;
    protected $myconn;

    public function __construct($dbName) {
        $this->database = $dbName;
    }

    public function getDbName() {
        return $this->database;
    }

}

//testing code

$db1 = new createConnection('database_1');
$db2 = new createConnection('database_2');

echo 'db1 database = ' . $db1->getDbName() . '<br />';
echo 'db2 database = ' . $db2->getDbName() . '<br />';

?>

Thanks for the info and I will give it a go; this is where I was getting stumped before as well; I could not understand the point of having to write the data into the class and knew there must be a way around it.

I am having trouble getting the idea of classes :confused:

Now this failed with an error - I added a “default” database:


<?php

class createConnection {

    protected $host = "localhost";
    protected $username = "user";
    protected $password = "password";
    protected $database = "database_0";
    protected $myconn;

    public function __construct($dbName) {
        $this->database = $dbName;
    }

    public function getDbName() {
        return $this->database;
    }

}

//testing code
$db0 = new createConnection();
$db1 = new createConnection('database_1');
$db2 = new createConnection('database_2');

echo 'db0 database = ' . $db0->getDbName() . '<br />';
echo 'db1 database = ' . $db1->getDbName() . '<br />';
echo 'db2 database = ' . $db2->getDbName() . '<br />';

?> 

Warning: Missing argument 1 for createConnection::__construct(), called in C:\xampp\htdocs\classes\ est.php on line 22 and defined in C:\xampp\htdocs\classes\ est.php on line 11

Notice: Undefined variable: dbName in C:\xampp\htdocs\classes\ est.php on line 12
db0 database =
db1 database = database_1
db2 database = database_2

I was expecting the database variable to be over written as it was for db1 and db2 but expected db0 to pick up the default value which it did not seem to do.

The error msg is telling you exactly what is wrong :slight_smile:

Line 12 is

$this->database = $dbName;

The constructor method expects a parameter to be passed to it ($dbName) and in your code

$db0 = new createConnection();

you are not passing anything to the constructor.

If you want to make it optional to pass paramaters, a method (in this case the constructor) needs code at the top of it to check what, if any, parameters are being passed to it and then act accordingly.

I assumed that was the problem but I expected it to work.

My problem is the the class I was going to write was going to have about 20 variables and I wanted standard settings for them all that I could overwrite with the class call if required; otherwise use the default setting.

So with this method I either have to include a value for evey variable to pass to the class or do some checking at the top of the class?

This may be why I am having a problem understanding classes in that they do not in my case offer any benifit over functions or including files with snippets of code.

class db {
protected $dbname = '';
function __construct($name = "default"){
$this->dbname=$name;
}

function getDbName(){
return $this->dbname;
}

}

$a = new db('db1');
echo $a->getDbName(); 
// db1


$b = new db();
echo $b->getDbName();
// default

That should answer your immediate question, you give the constructor (which is basically a function) a default argument as you create the blueprint for your object.

This is more to do with how functions work than OOP in PHP - and is a sure sign you need to swot up on Functions before attempting OOP, IMHO.

It wont take you long - there are not that many rules, but it is a kind of prerequisite.

Thanks for the info Cups I do not think I could have worked that out.

Another couple of things for me to do over the christmas break - functions and classes.