FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Taking parameters into functions
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Taking parameters into functions [message #175665 is a reply to message #175657] Mon, 17 October 2011 08:10 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 16/10/2011 14:56, houghi escribió/wrote:
> I have the following code:
>
> $File = "test.txt";
> function write_fifo($data,$File) {
> echo $data."<br>".$File."<hr>";
> $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
> fwrite($fh, $data);
> fclose($fh);
> }
> write_fifo("Something Here",$File);
>
> This works. What I want to ask is about the write_fifo() part. Is it
> possible to have something like the following:
> write_fifo("Something Here");
> instead of the line including $File.

That'd be pretty confusing. How about this?

$fw = new FifoWriter();
$fw->open('foo.txt');

$fw->write('Lorem ipsum dolor sit amet');
$fw->write('Consectetur adipisicing elit');

$fw->close();


The code would roughly look like this:

class FifoWriter{
private $fp;

public function open($file){
$fp = fopen($file, 'w');
if($fp){
$this->fp = $fp;
}else{
throw new Exception('Could not open file: ' . $file);
}
}

public function open($file){
$fp = fopen($file, 'w');
if($fp){
$this->fp = $fp;
}else{
throw new Exception('Could not open file: ' . $file);
}
}

public function write($data){
if( !fwrite($this->fp, $data) ){
throw new Exception('Could not write data');
}
}

public function close($file){
fclose($this->fp);
}
}



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Editing a combobox
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 00:42:26 GMT 2024

Total time taken to generate the page: 0.04151 seconds