PHWinfo banniere

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

Réponse
 
LinkBack Outils de la discussion
Vieux 10/11/2007, 20h32   #1
Oli Thissen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using a class to handle the output of another class

Hello everybody!

I'm having a little problem with the following: I wrote a class to
collect data (Let's call it DataCollector). Now I want the data to be
interpreted in various ways. My idea was to write an interface
(iVisualizer) so DataCollector could use every user defined
(MyVisualizer) class implementing it. Saying "MyVisualizer extends
DataCollector" is not an option, since the data should only be
collected once and then then being "rendered" by a variety of output
classes (to XML, to plain text, to PDF, etc.).

Here is the code example:

<php>
class DataCollector {
private $data

public function collectData ($someText) {
// Write data here
}

public function output($visualizer) {
$visualizer->output($this)
}
}

class MyVisualizer implements iVisualizer {
public function output ($dataCollector)
{
echo $dataCollector->data;
}
}

$a = new DataCollector();
$b = new MyVisualizer();
$a->collectData("foo");
$a->output($b);
</php>

Now, obviously calling "$a->output($b);" will get me in trouble, since
DataCollector::data is private. Making DataCollector::data public
would make it easy, but also "unsafe". Adding get & set methods will
add lots of methods, since $data is just an example for a large number
of properties.

I really hope to get some good ideas here. Thanks in advance!

Oli

  Réponse avec citation
Vieux 10/11/2007, 21h18   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a class to handle the output of another class

Oli Thissen wrote:
> Hello everybody!
>
> I'm having a little problem with the following: I wrote a class to
> collect data (Let's call it DataCollector). Now I want the data to be
> interpreted in various ways. My idea was to write an interface
> (iVisualizer) so DataCollector could use every user defined
> (MyVisualizer) class implementing it. Saying "MyVisualizer extends
> DataCollector" is not an option, since the data should only be
> collected once and then then being "rendered" by a variety of output
> classes (to XML, to plain text, to PDF, etc.).
>
> Here is the code example:
>
> <php>
> class DataCollector {
> private $data
>
> public function collectData ($someText) {
> // Write data here
> }
>
> public function output($visualizer) {
> $visualizer->output($this)
> }
> }
>
> class MyVisualizer implements iVisualizer {
> public function output ($dataCollector)
> {
> echo $dataCollector->data;
> }
> }
>
> $a = new DataCollector();
> $b = new MyVisualizer();
> $a->collectData("foo");
> $a->output($b);
> </php>
>
> Now, obviously calling "$a->output($b);" will get me in trouble, since
> DataCollector::data is private. Making DataCollector::data public
> would make it easy, but also "unsafe". Adding get & set methods will
> add lots of methods, since $data is just an example for a large number
> of properties.
>
> I really hope to get some good ideas here. Thanks in advance!
>
> Oli
>
>


First of all, a Visualizer object is not a "type of" DataCollector, so
trying to derive Visualizer from DataCollector would be incorrect.

You have the data items are private, as they should be (encapsulation).
So you need getter and setter methods for each one. The getter
methods return copies, and the setter methods validate the data before
actually storing it.

Yes, sometimes it means a bunch of setters and getter methods. But it's
also the correct way to do it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 11/11/2007, 02h39   #3
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a class to handle the output of another class

On Nov 10, 3:18 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Oli Thissen wrote:
> > Hello everybody!

>
> > I'm having a little problem with the following: I wrote a class to
> > collect data (Let's call it DataCollector). Now I want the data to be
> > interpreted in various ways. My idea was to write an interface
> > (iVisualizer) so DataCollector could use every user defined
> > (MyVisualizer) class implementing it. Saying "MyVisualizer extends
> > DataCollector" is not an option, since the data should only be
> > collected once and then then being "rendered" by a variety of output
> > classes (to XML, to plain text, to PDF, etc.).

>
> > Here is the code example:

>
> > <php>
> > class DataCollector {
> > private $data

>
> > public function collectData ($someText) {
> > // Write data here
> > }

>
> > public function output($visualizer) {
> > $visualizer->output($this)
> > }
> > }

>
> > class MyVisualizer implements iVisualizer {
> > public function output ($dataCollector)
> > {
> > echo $dataCollector->data;
> > }
> > }

>
> > $a = new DataCollector();
> > $b = new MyVisualizer();
> > $a->collectData("foo");
> > $a->output($b);
> > </php>

>
> > Now, obviously calling "$a->output($b);" will get me in trouble, since
> > DataCollector::data is private. Making DataCollector::data public
> > would make it easy, but also "unsafe". Adding get & set methods will
> > add lots of methods, since $data is just an example for a large number
> > of properties.

>
> > I really hope to get some good ideas here. Thanks in advance!

>
> > Oli

>
> First of all, a Visualizer object is not a "type of" DataCollector, so
> trying to derive Visualizer from DataCollector would be incorrect.
>
> You have the data items are private, as they should be (encapsulation).
> So you need getter and setter methods for each one. The getter
> methods return copies, and the setter methods validate the data before
> actually storing it.
>
> Yes, sometimes it means a bunch of setters and getter methods. But it's
> also the correct way to do it.
>


Everything Jerry said is correct. I would add that, if you have a lot
of private members (i.e. columns from a table in a database) you can
save yourself some work by taking advantage of the __get() and __set()
magic methods:

<http://www.php.net/manual/en/language.oop5.magic.php>

That way you don't need to write individual getters and setters for
every piece of data stored in your object.

  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 16h53.


É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,11342 seconds with 11 queries