You are here: Re: assigning values to class variables « PHP Language « IT news, forums, messages
Re: assigning values to class variables

Posted by ZeldorBlat on 07/30/07 00:33

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация