PHP 致命错误:无法将 DataAccess 类型的对象用作数组

发布于 2024-12-22 15:23:01 字数 1496 浏览 1 评论 0原文

我不明白为什么在 PHP 中出现以下错误:

致命错误:无法在第 16 行 /filename 中使用 DataAccess 类型的对象作为数组。

以下是该文件的相关代码:

class StandardContext implements IStandardContext
{
    private $dataAccess;

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

$config指的是以下内容:

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

这是 Config.php 的相关代码:

return array(

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

这是 DataAccess 对象的相关代码:

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

任何帮助将不胜感激,我对 PHP 相当陌生,并且绝对被难住了。

编辑:顺便说一句,我已经包含了以下代码来测试 StandardContext,它实际上有效(即它允许我对数据库进行比我显示的更深入的更改)

class StandardContext_index_returns_defined_list implements ITest
{
    private $dataAccess;

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

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']);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-12-29 15:23:01

这几乎就像您尝试使用单例模式,但是对于您实例化的每个 StandardContext 对象,您都在传递数据库参数(通过 $config 数组)。我认为发生的情况是您多次传递 $config 数组,第一次传递后 $config 不再是数组,而是 DataAccess 类的实例,这就是您收到该错误的原因。您可以尝试以下操作:

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

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');
        }
    }
俯瞰星空 2024-12-29 15:23:01

的问题

private $dataAccess;

这是您在此处检查数组对象

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

每当您在类内的方法外部声明时,它将被视为 Object ,因此您必须在方法内部声明或声明为方法本身,否则从类中删除实现。

您的 $dataAccess 是一个 Object ,因为您在方法外部声明了它,并且您的 new DataAccess($config['db']) 将返回一个 arrayObject 因为您实现了它,所以它尝试从 Object 转换为 arrayObject 会导致错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文