Re: Objects as array key names??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry for the long post, but I thought it would explain much of my thinking... Anyway, here we go...

Jeremy Mcentire wrote:
You may have meant the while loop to iterate over $row = fetch_object($res) ...

You are correct, I was simply trying to get the concept across. But you are correct, I should have used *_object() instead.

The basic idea is to always end up with an array of objects.


You could also create a data object associated with customer that makes use of overloading functions -- or whatever you kids are calling them these days to allow:

$customer = new Customer(78); // Load customer with ID 78.
$customer->loadLocations(); // Loads related locations.
$customer->location[n]; // Returns nth location.
$customer->location('home'); // Returns the location dubbed "home."
// You can have location loadLocations() if they're not already set.


This is how I currently do things. I have customer objects, contact objects, location objects, service objects, attribute objects, etc... and they all have similar getXXX() methods.

# Returns me all customers
$customers = new Customers();
$customers->getCustomers();
echo $customers; // __toString() returns the total number of customers


# Returns me a specific customer, or creates a new/empty customer object
$customer = new Customer([ int $customer_id ]);
$customer->getLocations(); // Returns locations objects
echo $customer; // __toString() returns the name of the customer


# Returns an object that will allow me to get all customer locations
$locations = new Locations( int $customer_id );
$locations->getLocations(); // Returns array of location objects
echo $locations; // __toString() returns the total number of locations


# Returns a location object, or creates a new/empty location object
$location = new Location([ int $location_id ]);
$location->getServices(); // Returns array of service objects
$location->getCustomer(); // Returns customer object
echo $location; // __toString() returns the name of the location


# Returns an object that contains all services to a given location
$services = new Services( int $location_id );
$services->getServices(); // Returns array of service objects
echo $services; // __toString() returns the total number of services


# Returns a service object, or creates a new/empty service object
$service = new Service([ int $service_id ]);
$service->getAttributes(); // Returns array of attribute objects
$service->getLocation(); // Returns location object
echo $service; // __toString() returns the name of the service


# Returns an object that contains all service attributes
$attributes = new Attributes( int $service_id );
$attributes->getAttributes(); // Returns array of attribute objects
echo $attributes; // __toString() returns the total number of attributes


# Returns a service object, or creates a new/empty service object
$attribute = new Attribute([ int $attribute_id ]);
echo $attribute; // __toString() returns the value of the attribute


I have many other types of objects: logs, CDR's, tasks, tickets, etc...

The data object would have to make use of __get, __set, and __call.

Each of the above objects use these functions and cleans data being extracted from and validates data being pushed into the said object.

The objects contains regex, string length, number range, and other types of checks before the data is accepted into the object.


Hereby, the locations aren't indexed by the object, but are elements in a member array.

Correct! And this member array/object is only created upon request. Therefor the overhead of creating said array/object is only felt when needed.

Jim Lucas

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux