在类中声明一个对象,给出一些奇怪的警告


declaring a object inside a class, giving some weird warning

我正试图在类Game中声明一个类型为Spell的对象,如下所示:

<?php
require 'Spell.php';
class Game
{
    public $Name;
    public $Spell;
    function Game()
    {
        $Name[0] = 0;
        $Spell = new Spell;
    }

这将返回此警告:

"警告:从中的空值创建默认对象"

我不知道为什么。

尝试以下操作:

class Game
{
    public $Name = array();
    public $Spell;
    function Game()
    {
        $this->Name[0] = 0;
        $this->Spell = new Spell();
    }
}

您应该使用

function Game()
{
    $this->Name = [ 0 ];
    $this->Spell = new Spell();
}

有关错误的详细信息,请查看此问题。