Correct way to pass over many variables in OOP design (PHP)

How to properly pass data to the class that is referenced in this structure:

                                  -Head
Index - PageBuilder - PageSection -Body
                                  -Foot

I want to send a lot of data to the head, for example, but I would like to avoid this:

new PageSection('head','how to cook',$keywords,$description,$script,$css);

The above looks terrible to manage and change. And there is more data that will need to be transferred, not shown in this example!

The Head, Body, and Foot classes need a lot of user-supplied data, but the best way is to send that data. Be aware that some data needs to be hard-coded and some needs to be installed based on the database or in some cases rely on default settings.

+2


source to share


4 answers


Why not create an object that represents your parameter set? This way you are just passing one object, but that contains your individual parameters. One of the advantages is that you can easily refactor to add / remove data sent to the method, but not modify method calls as they just process the parameter object.

Below is an example (contrived) (in Java as I don't know PHP)



// build the parameters from command-line args 
// (you could build from a database or XML or whatever)
PageParameters p = PageParameters.fromArgs(args);

PageSection page = new PageSection(p);

      

PageParameters

will contain the properties needed to construct the object PageSection

in one form or another - not necessarily the form that it will PageSection

save itself. PageParameters

may have functionality of its own that is not relevant PageSection

(e.g., parsing command line arguments, health check values, etc.)

+3


source


Following the advice of Brian Agnew and Nicky De Mayer, use both setters and getters, and an array or object in the constructor. So my selection framework (Zend) handles this exact problem.

There are several advantages to using both methods over using only one of them.

  • Ease of initial configuration. If you were only using setters and getters, you would need to set each individual property in your code after the object is created.
  • By using getters and setters, you get encapsulated logic that will manage every property you need to manage inside those objects.


Zend Framework uses a Config object, which can be created using an array and can be added or removed from the dynamic. But when a config object is passed to a constructor, it is immediately converted to an array and used as such. Thus, you can use the Config object if it makes sense (say, as loading in the settings from the database or from the configuration file) or just use an array if it makes sense (say, as setting parameters in the code of a simple Instantiation object).

In addition, Zend Framework provides getters and setters so that you can freely modify an object in code at any time. Otherwise, you will have to destroy the existing object and then re-instantiate it using the configured parameters.

+3


source


This will probably be the definition of the object that contains your variables. Thus, you can safely refer to a specific property by its name. You can also use map or array. For example jQuery wildly uses the params object for this purpose, so in your code you can have (mocked syntax)

doSomething(props) {
  props.head
  props.body
  props.foot
}

      

0


source


You can use setters instead of passing everything through the constructor.

$section = new PageSection('head');
$section->addKeyword();
$section->setDescription();

      

you can get an array of the given keywords, edit the array and reset it:

$keywords = $section->getKeywords();
//edit them
$section->setKeywords($keywords);

      

or you can create even more subclasses, but watch out for complexity and unnecessary subclassing ...

-1


source







All Articles