在PHP中创建嵌套类的正确方法是什么?

时间:2012-12-27 21:03:54

标签: php class multidimensional-array nested

我需要创建一个多维数组的嵌套结构类,这是我的数组:

Array
(

    [days] => Array
        (
            [0] => Array
                (
                    [rows] => Array
                        (
                            [0] => Array
                                (
                                    [activity_id] => 1
                                    [name] => Activity 2
                                    [city] => London
                                    [info] => fsdsdshgsfd 
                                )

                            [1] => Array
                                (
                                    [activity_id] => 3
                                    [name] => Activity 1
                                    [city] => London
                                    [info] => fsdhgsfd 
                                )
                        )

                )
           [1] => Array
                (
                    [rows] => Array
                        (
                            [0] => Array
                                (
                                    [activity_id] => 3
                                    [name] => Activity 1
                                    [city] => London
                                    [info] => fsdhgsfd 
                                )

                                ...

                                )
                        )
                )

                ...
       )
)

我一直在尝试重写我的代码以使其成为类驱动的,但我正在努力解决这个问题,构建类结构Itinerary->Days->Rows以替换数组的正确方法是什么?我尝试过类似的东西,我不确定它是否有意义,我真的不明白它是如何完成的:

class Itinerary
{

    private $days = array();

    public static function addDay($day) {
        $this->$days[] = new ItineraryDay($day);
    }
}

class ItineraryDay implements Countable
{

    private $rows = array();

    public static function addRow($row) {
        $this->$rows[] = new ItineraryRow($row);
    }

    public function count()
    {
        return count($this->rows);
    }
}

class ItineraryRow implements Countable
{

    private $name;
    private $city;
    ...

    function __get($key)
    {
        ...
    }

    function __set($key, $value)
    {
        ...
    }

    public function count()
    {
        return count($this->rows);
    }
}


$itinerary1 = new Itinerary(); 

$day1 = new ItineraryDay(); 
$itinerary1->addDay($day1);

$row1 = new ItineraryRow(); 
$day1->addRow($row1);

有人可以指导我吗?

1 个答案:

答案 0 :(得分:2)

这实际上取决于你最终想要对所述结构做什么,但对于一般的想法,我通常做这样的事情:

class Itinerary implements Countable
{
    private $days;

    public function __construct( array $days = array() )
    {
        $this->setDays( $days );
    }

    public function addDay( ItineraryDay $day )
    {
        $this->days[] = $day;
    }

    public function setDays( array $days )
    {
        $this->days = array();
        foreach( $days as $day )
        {
            $this->addDay( $day );
        }
    }

    public function count()
    {
        return count( $this->days );
    }
}

class ItineraryDay implements Countable
{
    private $rows;

    public function __construct( array $rows = array() )
    {
        $this->setRows( $rows );
    }

    public function addRow( ItineraryRow $row )
    {
        $this->rows[] = $row;
    }

    public function setRows( array $rows )
    {
        $this->rows = array();
        foreach( $rows as $row )
        {
            $this->addRow( $row );
        }
    }

    public function count()
    {
        return count( $this->rows );
    }
}

class ItineraryRow
{
    private $id;
    private $name;
    private $city;
    private $info;

    public function __construct( $id, $name, $city, $info )
    {
        $this->id   = $id;
        $this->name = $name;
        $this->city = $city;
        $this->info = $info;
    }

    /* ... */
}

然后将其与当前数据数组的结构一起使用:

$days = array();
foreach( $originalData[ 'days' ] as $days )
{
    $rows = array();
    foreach( $days[ 'rows' ] as $row )
    {
        $rows[] = new ItineraryRow( $row[ 'activity_id' ], $row[ 'name' ], $row[ 'city' ], $row[ 'info' ] );
    }
    $days[] = new ItineraryDay( $rows );
}
$itinerary = new Itinerary( $days );

或者:

$itinerary = new Itinerary;
foreach( $originalData[ 'days' ] as $days )
{
    $day = new ItineraryDay;
    foreach( $days[ 'rows' ] as $row )
    {
        $row = new ItineraryRow( $row[ 'activity_id' ], $row[ 'name' ], $row[ 'city' ], $row[ 'info' ] )
        $day->addRow( $row );
    }
    $itinerary->addDay( $day );
}

因此,您可以将“子”对象传递给构造函数(构造新对象的方法),或者在构造之后使用方法添加它们。如果您希望对象是不可变的,这意味着您不希望对象在构造后接受更多行/天,只需创建addDaysetDaysaddRowsetRows方法protectedprivate因此只允许通过构造函数传递“child”对象。

请注意,正如PeeHaa已经提到的那样,您不需要静态方法,因为它们操作类宽,而不是类(对象)的单个实例。事实上,你甚至不能按照预期的方式使用静态方法,因为$this仅在对象上下文中可用,而不是在类范围的上下文中。

但是,说实话,这个问题有点模糊,无法正确回答。我们必须更详细地介绍一下如何构建对象,以及以后如何使用它们。