Proper way to pass along many variables in an OOP design (PHP)?

So you simply pass one object, but that contains your individual parameters. One advantage is that you can refactor easily to add/remove data sent to the method, but not change the method invocations since they're just handling the parameter object A (contrived) example follows (in Java, since I'm not PHP-aware) 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 would contain the properties required to construct a PageSection object in some form - not necessarily the form that PageSection would store them itself PageParameters could have functionality itself that doesn't belong in PageSection (e.g. Command line args parsing, sanity-checking values etc. ).

So you simply pass one object, but that contains your individual parameters. One advantage is that you can refactor easily to add/remove data sent to the method, but not change the method invocations since they're just handling the parameter object. A (contrived) example follows (in Java, since I'm not PHP-aware) // 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 would contain the properties required to construct a PageSection object in some form - not necessarily the form that PageSection would store them itself. PageParameters could have functionality itself that doesn't belong in PageSection (e.g. Command line args parsing, sanity-checking values etc. ).

This brings back the same problems, but just in another place... – Nicky De Maeyer Oct 18 '09 at 20:42 Not when you pass around that data across more than one object and/or through different layers. – Brian Agnew Oct 18 '09 at 20:45 I agree, I would refactor the parameters into a data-object (all member variables are public and very few (no? ) methods) and then pass an instance of such an object to the PageSection class.

– Morningcoffee Oct 18 '09 at 20:55 Could you show a simple example? Would I use an interface? – Allen Oct 18 '09 at 20:56 look at the Q, hes talking about passing params to the constructor.

You state the following: Create an object, set all the params you need to pass to the constructor, pass the objct to the constructor, inside the constructor get all the properties off the object and set them as properties on itself. + save the saved object to be able to pass it around some more (in which case you could pass the class itself, since it has all the same properties) Doesn't seem very logical? – Nicky De Maeyer Oct 18 '09 at 20:58.

Following the suggestions from Brian Agnew and Nicky De Maeyer, use both setters and getters and an array or an object in the constructor. My framework of choice (Zend) handles this precise problem in this manner. Using both methods presents a couple of advantages over using just one of them.

Ease of initial configuration. If you were to use setters and getters solely then you would have to set each individual property in your code after the object is instantiated. Using getters and setters, you get encapsulation of the logic that will drive each property that you need to manage within these objects.

Zend Framework uses a Config object that can be instantiated with an array and can be added to or removed from dynamically. But when a config object is passed to a constructor, then it is immediately converts to an array and is used as such. In this manner, its possible to use a Config object if that makes sense (say like loading in settings from the database or from a config file) or to just use an array if that makes sense (say like setting the parameters in code just object instantiation).

Additionally, Zend Framework provides the getters and setters so that you can freely modify the object in code at any time. Otherwise you would have to destroy the existing object and then re-instantiate with the adjusted parameters.

That probably would be defining the object that contains your variables. This way you can safely refer to the particular property by its name. You can also use map or array.

For example, jQuery wildly using params object for such purpose, so in your code you can have (mocked syntax) doSomething(props) { props. Head props. Body props.

Foot }.

You could use setters instead of passing everything through the constructor. $section = new PageSection('head'); $section->addKeyword(); $section->setDescription(); you could get an array of the set keywords, edit the array and reset it: $keywords = $section->getKeywords(); //edit them $section->setKeywords($keywords); or you could create even more subclasses, but watch out for complexity and unneccesary subclassing...

The issue here is that I cant be calling functions that way because the Section object is being declared inside another object. – Allen Oct 18 '09 at 21:02 Using setters has the problems of (1) order in which they are called (2) not all setters have to be called by the client. – Alex Nov 18 '09 at 18:38.

Stored in variables is instead stored in these properties. (essentially variables) are all wrapped up and isolated within the object. To one of the objects the other is not affected.

Property, we are actually sending a message to the object. Look at this line of code that shows what’s really happening. When you send it a message to do so.

Corresponding Let method for setting each property. Here’s what it looks like. This is only general.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions