使PHP类中的每个函数都引用相同的数据库行

Looking to make a class that will be for 'customers'. The purpose of this class is to pass in some details, and then either add a user or update an existing one.

After this, I might also want to do to more things with this 'customer'.

Im new to classes so need to know how to always refer to the same row no matter what i'm doing with the current initiated class.

Basic use:

$customer = new Customer($customer_details);

When creating a new instance of this class, I want it to straight away add / update a row.

However, as well as this later on I might want to update a specific field:

$customer->setValue('firstname','Craig');

But I dont want to have to do another lookup to get the row ID etc in this fucntion. I want $customer to always reference the user given the $customer_info.

Assuming you want setValue() to update the database, I would advise creating a field in your Customer class that contains the identifier for the row in the database.

/**
 * Database identifier.
 *
 * @var string/int
 */
 private $id;

Have your constructor set the value of the identifier on object creation using $this->id = $id. Then your setValue() call can use that identifier to update the information in the database.

EDIT

If you are creating a new Customer instead of updating an existing one (passing new data to the constructor) the database controller that you are using should have a means to get the ID of the new row (given it is an auto-incremented value). Otherwise the ID is something that you are generating, in which case you could just set the field value in the class.

I think you're referring to the Active Record Pattern. You can find many implementations in PHP, such as Propel.