代码之家  ›  专栏  ›  技术社区  ›  unrelativity

构造函数[duplicate]中不存在PHP类

php
  •  2
  • unrelativity  · 技术社区  · 15 年前

    致命错误: 对非对象调用成员函数query()

    SPConfig SPClasses

    $sp = new SPClasses();
    
    class SPClasses
    {
        public $db, $config, $page, $users;
    
        function __construct()
        {       
            // The SPDatabase class [...]
            $this->db = new SPDatabase();
    
            // The SPConfig class [...]
            $this->config = new SPConfig();
    
            // [...]
    
            // The SPTemplate class [...]
            $this->page = new SPTemplate();
    
            // The SPUsers class [...]
            $this->users = new SPUsers();
        }
    }
    
    class SPConfig
    {
        function __construct()
        {
            global $sp;
            $configquery = $sp->db->query("SELECT * FROM config", 'Retrieving the configuration');
            while($row = mysql_fetch_array($configquery))
            {
                try
                {
                    $this->{$row['name']} = $row['value'];
                }
                catch (Exception $e)
                {
                    $debug_log[] = array(
                        'type' => 'error',
                        'success' => false,
                        'text' => 'The config setting <i>' . $row['config_value'] . '</i> has an invalid name.'
                    );
                }
            }
        }
    
    
        public function update()
        {
            $this->__construct();
        }
    }
    

    有人知道为什么我不能进入吗 $sp->db 建造师?

    谢谢。

    2 回复  |  直到 12 年前
        1
  •  4
  •   cHao    15 年前

    直到 $sp SPConfig ,尝试访问仍然为空的 $标准普尔 .

    为了让它工作,构造函数需要通过 $this $this->db

        2
  •  3
  •   ZeissS    15 年前

    你有个鸡蛋问题。在分配全局 $sp 你把 SPClasses 实例化的对象 SPCOnfig 哪个入口 尚未分配。

    SPClass类 SPConfig 同学们,你们很好。