OOP (uso beneficioso)

por mi pregunta enCómo usar la POO de una manera beneficiosa. Supongo que como ejemplo una CESTA a la que su propietario (Tom) que tiene una determinada DIRECCIÓN (NY) puede agregar ARTÍCULOS (Bicicleta, Coche). Finalmente se imprime una FACTURA conteniendo toda esta información.

Mi problema es: ¿Cómo manejar la recopilación de la información deseada (aquí: propietario, ciudad, cantidad de artículos) de varios objetos? Porque creo que es estúpido hacer esto manualmente como se hace a continuación (ver 4.), ¿no es así? (Más aún porque la cantidad de información aumenta en la realidad).

Entonces, ¿cuál es la "forma limpia" para crear la factura / recopilar la información necesaria en este ejemplo?

<code><?php
$a = new basket('Tom','NY');
$a->add_item("Bike",1.99);
$a->add_item("Car",2.99);

$b = new bill( $a );
$b->do_print();
</code>

1.

<code>class basket {

    private $owner = "";
    private $addr = "";
    private $articles = array();

    function basket( $name, $city ) {
        // Constructor
        $this->owner = $name;
        $this->addr = new addresse( $city );

    }

    function add_item( $name, $price ) {
        $this->articles[] = new article( $name, $price );
    }

    function item_count() {
        return count($this->articles);
    }

    function get_owner() {
        return $this->owner;
    }

    function get_addr() {
        return $this->addr;
    }

}
</code>

2.

<code>class addresse {

    private $city;

    function addresse( $city ) {
        // Constructor
        $this->city = $city;
    }

    function get_city() {
        return $this->city;
    }

}
</code>

3.

<code>class article {

    private $name = "";
    private $price = "";

    function article( $n, $p ) {
        // Constructor
        $this->name = $n;
        $this->price = $p;
    }   

}
</code>

4.

<code>class bill {

    private $recipient = "";
    private $city = "";
    private $amount = "";

    function bill( $basket_object ) {

        $this->recipient = $basket_object->get_owner();
        $this->city = $basket_object->get_addr()->get_city();
        $this->amount = $basket_object->item_count();

    }

    function do_print () {
        echo "Bill for " . $this->recipient . " living in " . $this->city . " for a total of " . $this->amount . " Items.";
    }

}
</code>

Respuestas a la pregunta(3)

Su respuesta a la pregunta