Question

I cannot figure out why I am getting the following error in PHP:

Fatal error: Cannot use object of type DataAccess as array in /filename on line 16.

Here is the relevant code for the file:

class StandardContext implements IStandardContext
{
    private $dataAccess;

    // (CON|DE)STRUCTORS
    function __construct($config)
    {
        $this->dataAccess = new DataAccess($config['db']); //this is line 16
    }

$config refers to the following:

$config = require(dirname(__FILE__)./*truncated*/.'Config.php');

Here is the relevant code for Config.php:

return array(

    // Database connection parameters
    'db' => array(
        'host' => 'localhost',
        'name' => 'visum',
        'user' => 'root',
        'password' => ''
    )
);

Here is the relevant code for the DataAccess object:

class DataAccess
{
    private $link;
    private $db;

    function __construct($dbConfig)
    {            
        $this->link = mysql_connect( $dbConfig['host'], $dbConfig['user'], $dbConfig['password'] ) or die(mysql_error());
        $this->db = $dbConfig['name'];
        mysql_select_db($this->db) or die(mysql_error());
    }

Any help would be greatly appreciate, I am fairly new to PHP and am absolutely stumped.

Edit: BTW, I have included the following code to test StandardContext, which actually works (ie. it allows me to make changes to my database farther down than I have shown)

class StandardContext_index_returns_defined_list implements ITest
{
    private $dataAccess;

    function __construct($config)
    {
        $this->dataAccess = new DataAccess($config['db']);
    }
Was it helpful?

Solution

It's almost like you are trying to use a singleton pattern, but for every StandardContext object you instantiate, you are passing in database parameters (via $config array). I think what's happening is that you are passing the $config array more than once, after the first pass the $config is no longer an array, but an instance of the DataAccess class, which is why you are getting that error. You can try the following:

class StandardContext implements IStandardContext
{
    private $dataAccess;

    // (CON|DE)STRUCTORS
    function __construct($config)
    {
        if ($config instanceof DataAccess) {
            $this->dataAccess = $config;
        } elseif ((is_array($config)) && (array_key_exists('db', $config))) {
            $this->dataAccess = new DataAccess($config['db']); 
        } else {
            throw new Exception('Unable to initialize $this->dataAccess');
        }
    }

OTHER TIPS

this is problem with your

private $dataAccess;

check the array object here

http://www.php.net/manual/en/class.arrayobject.php

whenever you declare outside a method inside class, it will consider as Object , so you have to declare inside method or declare as method itself else remove implements from your class.

your $dataAccess is an Object , because you declare it outside the method and your new DataAccess($config['db']) will return an arrayObject because you implements that, so it is trying to convert from Object to arrayObject leads an error

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top