/    Sign up×
Community /Pin to ProfileBookmark

Design Decision – PHP Class or Array

Hello,

Need help making a decision, not sure which way to go with the class I’m trying to design. The class essentially allows variables to be set, retrieved and validated.

An example of the current code I’m using is

[code=php]
Class MyClass{
$details = array(
“address” => array(“required” => true, “maxlength” => 50, “value” => “”),
“town” => array(“required” => true, “maxlength” => 20, “value” => “”),
“state” => array(“required” => true, “maxlength” => 20, “value” => “”),
);
//other code
}
[/code]

but I’m not sure if I should be using a class to describe these variables for instance;

[code=php]
class Detail {
var $required;
var $value;
var $maxlength;
//constructor etc
}

class MyClass {
var $address = new Detail(true, 10, 20);
var $town = new Detail(false, 10, 20);
// etc
}
[/code]

I don’t know if you guys think that this is a minor issue or not but I’m not really sure which way is better.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 30.2007 — [CODE]class MyClass {
var $address = [COLOR="Red"][B]new Detail(true, 10, 20)[/B][/COLOR];
var $town = [COLOR="Red"][B]new Detail(false, 10, 20)[/B][/COLOR];
// etc
}
[/CODE]
[/QUOTE]
A default value must be a constant expression, not (for example) a variable, a class member or a function call.
Copy linkTweet thisAlerts:
@NogDogApr 30.2007 — I think it comes down to whether or not you want to think of each form field as an object or only as an attribute of the form. If you foresee any possibility that you would want to associate particular behaviors (i.e. methods) with particular types of fields or all fields, then I would probably pursue the separate class approach. If each field definition will simply be a static set of attributes and values, then I see no particular functional gain by making it into an object, though it still has a certain aesthetic appeal.
×

Success!

Help @jknow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...