PruitIgoe
PruitIgoe

Reputation: 6384

PHP passing value to a class

I have a class called DatabaseController which I've set up as so:

<?php

class DBController {

    public $dbName;

    function _controller($passedDBName) { 
        $dbName = $passedDBName;
    }

    function dbConnect() { 
        print $this->$dbName;
    }



} //end of class       
?>

and I am calling it this way:

<?php

    //make new database connection
    $dbManager = new DBController("somevalue");
    $dbManager->dbConnect();


?>

but I keep getting this error:

<b>Fatal error</b>:  Cannot access empty property in 

What am I doing wrong?

Thanks

Upvotes: 0

Views: 76

Answers (2)

flowfree
flowfree

Reputation: 16462

In the constructor, $dbName = $passedDBName; should be $this->dbName = $passedDBName;.

Update:

  1. $this->$dbName should be $this->dbName.
  2. _controller() should be __construct().

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318728

Use $this->dbName - otherwise you try to access a field whose name is stored in $dbName. You also need to fix your constructur to assign to $this->dbName instead of $dbName.

class DBController {
    public $dbName;
    function _controller($passedDBName) { 
        $this->dbName = $passedDBName;
    }
    function dbConnect() { 
        print $this->dbName;
    }
}

Upvotes: 3

Related Questions