I am just about to write a method to convert some billing data into an invoice.
So say i have and an array of objects that contain the data necessary to create the invocie items.
While in the billing controller Which of the following way is correct
$invoice = new Invoice();
$invoice->createInvoiceFromBilling($billingItems);
Then in the Invoice Class
Public Function createInvoiceFromBilling($billingItems)
{
$this->data = $billingItems;
OR
Invoice::createInvoiceFromBilling($billingItems)
Then in the Invoice Class
Public Function createInvoiceFromBilling($billingItems)
{
$invoice = new Invoice();
$invoice->data = $billingItems;
Which way is the correct way?
Regards
The method written first is better because in the second your code will generate object for invoice each time it is being called.
As tereško pointed out in the comments section above, you should look into using the Factory pattern. A good (and simple) real-world-based example from the linked source:
As you can see, it is AutomobileFactory that actually creates instances of Automobile.