mardi 14 décembre 2021

PHP/OOP how to design classes for a small project

I need a bit of advice, how to design my PHP classes. It should be a few classes building/re-building a menu for another application.

Initial data:

  1. a big json file, menu json chunks are repetitive so we have a global menu
{json data}
[menu_column1]
{json data}
[menu_column2]
{json data}
.....
  1. menu item template
{json data}
[menu_item_name]
{json data}
[menu_item_url]
{json data}
[menu_item_id]
{json data}
  1. a list of menu items name url id

Note: Cannot edit the menu with original visual editor, due to many menu items, it just get stuck.

I wrote a simple script (around 100 lines of code), that

  • opens 3 files
  • sorts the menu items (just for printing to screen)
  • finds duplicates by name or by url
  • resorts menu items horizontally or vertically (4 columns)
  • generates json of each menu item - column
  • generates the menu json (4 columns).

=========

I'm trying to build the same into OOP, right way, using patterns where is possible.

At the moment of building classes appeared some questions:

  • where and how to store configurations (in my original code have config.php with constants)
  • does the input/output need a separate class
  • how to operate with files from classes (right way)
  • how many classes to create and what is the purpose of each class

Any suggestions how to design my classes would be welcome.

Aucun commentaire:

Enregistrer un commentaire