PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.comp.lang.php > assigning values to class variables
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
assigning values to class variables

Réponse
 
LinkBack Outils de la discussion
Vieux 29/07/2007, 19h19   #1
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut assigning values to class variables

PHP 5.2.3

Sorry for that odd title. But what I'm wondering about is assigning values
stored in a database to class variables. Lets say a class has the variable
$name and when instantiation of the class this $name variable should get a
value from the database..

I know it is possible to do a simple select, and fill the $name with the
returing result.

I'm just wondering if PHP may have a better approach?


  Réponse avec citation
Vieux 30/07/2007, 01h33   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning values to class variables

On Jul 29, 2:19 pm, "Jeff" <it_consulta...@hotmail.com.NOSPAM> wrote:
> PHP 5.2.3
>
> Sorry for that odd title. But what I'm wondering about is assigning values
> stored in a database to class variables. Lets say a class has the variable
> $name and when instantiation of the class this $name variable should get a
> value from the database..
>
> I know it is possible to do a simple select, and fill the $name with the
> returing result.
>
> I'm just wondering if PHP may have a better approach?


You basically just described what you need to do. Any way you slice
it you'll need to query the database, get the value from the result,
and assign it to a class variable.

The only advice I can give you is to separate the construction of your
object from the part that does the database stuff. Many are tempted
to do something like this:

class Foo {
protected $name;

public function __construct() {
//code to get the name from the database
//and set the instance variable

//other stuff that the constructor needs to do
}
}

Obviously that will work, but as a matter of good OOP style you really
should do it like this:

class Foo {
protected $name;

public function __construct() {
$this->initName();
//other stuff that the constructor needs to do
}

protected function initName() {
//code to get the name from the database
//and set the instance variable
}
}

There was actually a very lengthly discussion on this topic recently.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,06901 seconds with 10 queries