/    Sign up×
Community /Pin to ProfileBookmark

Declaring class properties?

Hi,

I have a class which inserts a row into a database. The values come from an associative array passed as an arguement to a method in the class.

Should i assign the elements of the array as properties to the class before using them in the query or is it ok to just use them directly in the query.

For instance, should i do this:

[CODE]class Database{
public function insertRow($array){
$this->item1 = $array[0];
$this->item2 = $array[1];
$sql = “INSERT INTO table VALUES ($this->item1, $this->item2) WHERE id = 1”;
}
}[/CODE]

..or is it ok to just do this:

[CODE]class Database{
public function insertRow($array){
$sql = “INSERT INTO table VALUES ($array[0], $array[1]) WHERE id = 1”;
}
}[/CODE]

..just looking for best practice advice and pros/cons

Thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 10.2009 — The only reason to add store them as object variables is if there is some reason to keep them so that other methods can access them (or you want them to be publicly accessible from outside of the object). If there is no such need, then there is no reason to save them as such.
Copy linkTweet thisAlerts:
@pavsidauthorOct 10.2009 — The only reason to add store them as object variables is if there is some reason to keep them so that other methods can access them (or you want them to be publicly accessible from outside of the object). If there is no such need, then there is no reason to save them as such.[/QUOTE]

That'll do for me, thanks a bunch (again!)
×

Success!

Help @pavsid 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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