Note that there are some explanatory texts on larger screens.

plurals
  1. POOOP classes and external lists of data
    text
    copied!<p>When programming OO in PHP i never know exactly how to map a class to simple lists of data. I will try to make a simple example wich i am running into every day:</p> <p>First the MySQL table creates:</p> <pre><code>/* create product table */ CREATE TABLE `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `description` text COLLATE utf8_unicode_ci, `price` decimal(10,0) NOT NULL, `brand` int(11) NOT NULL, `deliverytime` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci /* data list for all kind of brands */ CREATE TABLE `brand` ( `id` int(11) NOT NULL AUTO_INCREMENT, `brand` varchar(100) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci /* brand data*/ insert into `brand`(`id`,`brand`) values (1,'nike'),(2,'adidas'),(3,'diesel'), (4,'dkny'),(5,'lacoste'); /* data list for deliverytime */ CREATE TABLE `deliverytime` ( `id` int(11) NOT NULL AUTO_INCREMENT, `deliverytime` varchar(100) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci /* deliverytime data */ insert into `deliverytime`(`id`,`deliverytime`) values (1,'1 day'),(2,'2 days'),(3,'3 days'),(4,'4 days'),(5,'5 days'),(6,'6 days'),(7,'1 week'),(8,'1 - 2 weeks'),(9,'2 - 3 weeks'),(10,'4 - 5 weeks'); </code></pre> <p><strong>Then</strong> i create the product class.</p> <pre><code> class Product{ private $name, $description, $price, $brand $deliverytime; public function __construct(){ // etc etc } public function save(){ // save the product } } </code></pre> <p><strong>Now the big question(s) are:</strong></p> <p>How should i handle $brand and $deliverytime in my Product class? Should i make a Brand and DeliveryTime object (wich in turn are responsible for fetching the right brand and or deliverytime data)?</p> <p>And what about saving the Product object? How should i handle the brand and deliverytime data? </p> <p>What is the beste practice or pattern to handle this kind of situations?</p> <p>Sorry for this noobish question but i wasnt sure where to look for (tags to search for) :/</p> <p><strong>EDIT:</strong></p> <p>Ok lets say i dont want to use somekind of ORM framework (Doctrine, dORM, redbean etc) since it would be a gigantic overkill for my little system + i am realy want to know how to create the mapping myself for learning purposes... any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload