You are here: Using a class to handle the output of another class « PHP Programming Language « IT news, forums, messages
Using a class to handle the output of another class

Posted by Oli Thissen on 11/10/07 19:32

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

 

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

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