开发者

proper way of using output of one class in another class

开发者 https://www.devze.com 2023-02-28 09:40 出处:网络
I am using the following code to produce an array: // Create new customer object $customers = new customers;

I am using the following code to produce an array:

// Create new customer object
$customers = new customers;

// Array of user ids 
$customers->ids = array(34,23,78);  

// Process customers
$customers->processCustomers();

The above code will output an array full of needed user information based on the users passed to it.

Now I need to take further action to this new customer array in the following code:

// Create new parsing object
$parseCustomers = new parse;

// Array of customer data 
$parseCustomers->customers = ???????????    

// Finalize new transaction
$parseCustomers->finalizeTransaction();

I need to pass the array from the first class ou开发者_如何学运维tput into the second and I'm wondering what best practice is.

This customer array can be very large at times, so ideally I wouldn't have 2 variables containing the same array at any time.


If you mean the processed array from the $customers object, you need some public method to get that data.

Let's assume you have a public method getData(). Your code would then look like this...

// Create new parsing object
$parseCustomers = new parse;

// Array of customer data 
$parseCustomers->customers = $customers->getData();    

// Finalize new transaction
$parseCustomers->finalizeTransaction();


There are several different ways you can handle this. Here are two:

  1. You could return the MySQL result set from your class. I would run your query with mysql_unbuffered_query() so that it only uses up memory as you need the records.

  2. When you grab your results, you could pass in your customers by reference:

Here's the code for #2:

// Create new parsing object
$parseCustomers = new parse;

// Array of customer data (not needed)
// $parseCustomers->customers = ???????????    

// Finalize new transaction
$parseCustomers->finalizeTransaction($arrCustomers);

And your function declaration would change to this:

function finalsizeTransaction(&$arrCustomers) {

    // Do your processing

}

This would allow you to use the ONE array of customers, eliminating your duplication. If you're dealing with really large data sets, so much that it could cause PHP to error out when it runs out of memory, I would use the unbuffered query method.

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消