human
human

Reputation: 736

What is the right way to create nested classes in PHP?

I need to create a nested structure class of a multidimensional array, this is my array:

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 
                                )

                                ...

                                )
                        )
                )

                ...
       )
)

I have been trying to rewrite my code to make it class-driven, but I am struggling with that, what is the right way to build a class structure Itinerary->Days->Rows to replace an array? I tried something like this, I am not sure if it makes sense, I don't really understand the way how it has to be done:

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

Can someone guide me?

Upvotes: 0

Views: 713

Answers (1)

Decent Dabbler
Decent Dabbler

Reputation: 22783

It really depends what you ultimately want to do with said structure, but for a general idea I typically do something like this:

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

    /* ... */
}

Then using it with the structure of your current array of data:

$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 );

Or:

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

So, you can either pass "child" objects to the constructor (the method that constructs a new object), or add them with methods after construction. If you want the objects to be immutable, meaning you don't want to allow the objects to accept any more rows / days after construction, just make the addDay, setDays, addRow and setRows methods protected or private thereby only allowing passing "child" object through the constructors.

Be aware that, as PeeHaa already mentioned, you don't want static methods, because they operate class wide, not on individual instances of classes (objects). As a matter of fact, you cannot even use static methods the way you intended, because $this is only available in object context, not in class wide context.

But, to be honest, the question is a little bit to vague to be answered properly. We'd have to have a little more details about how you are going to construct the objects, and how you are going to use them later on.

Upvotes: 2

Related Questions