I have a problem that would be perfectly solved with multiple constructors, but since those aren't allowed in PHP, I'd like to see how some of you are solving this problem:
Say I have a database full of customer records and in my PHP code I create a class called Customer as a data container. The problem is that there are two ways I want to construct the object:
1.) I want to create a Customer object with data from an existing record in the database.
2.) I want to create a Customer object AND insert a new record into the database.
For #1 I would create a constructor that would only take a database ID, query the db and populate the object's properties:
$customer = new Customer($id);
For #2 I would create a constructor that would take all the values so that it can use those to insert a new record in the DB and populate the object's properties.
$customer = new Customer($first_name, $last_name);
The only thing I can think of is to create a function that inserts the record, then run mysql_insert_id() to get ID of last inserted record, and use that ID to construct the class:
<?php
insertCustomerRecord($first_name, $last_name);
$id = mysql_insert_id();
$customer = new Customer($id);
?>
But that just seems sloppy to me. Anyone have any better solutions to this problem?